Thank you! After a lot of trial and error I got it working.
I couldn’t get it with the Corona SDK build 268, because the headers in http request throw an error. But I download the last one (307) and it worked perfectly.
I share here my code for if someone tries do do the same. I don’t know if its the best but it works for me.
- Lua script:
[blockcode]
function synchronizeData()
local stmt = dbMng.db:prepare(“SELECT * FROM users ORDER BY users.idLista ASC”)
local idListe
local xml = “\n”
for row in stmt:nrows() do
if(row.idLista ~= idListe) then
if(idListe ~= nil) then
xml = xml…"\n"
end
idListe = row.idLista
xml = xml… “\n”
end
xml = xml…"\n"
xml = xml…""…row.name…"\n"
xml = xml…""…row.surname…"\n"
xml = xml…"\n"
xml = xml…"\n"
end
xml = xml…"\n"
xml = xml…""
stmt:finalize()
params.headers = {[“Content-Type”]=“text/xml;charset=utf-8”}
params.body = xml
network.request(“http://localhost:8888/pms_listing/sync_data.php”, “POST”, syncronizeListener, params)
end
[/blockcode]
Then my php script is like this:
[blockcode]
<?php
require\_once('cnx.php');
/\*Contains the Class "http\_request"
From http://www.fijiwebdesign.com/blog/acess-the-http-request-headers-and-body-via-php.html
\*/
include("http\_request.php");
/\*
This xml parser is from vladimir\_wof\_nikolaich\_dot\_ru:
http://php.net/manual/en/function.xml-parse.php
\*/
function my\_xml2array($contents)
{
$xml\_values = array();
//I comment the next line because I pass the XML directly
//$contents = file\_get\_contents($\_\_url);
$parser = xml\_parser\_create('');
if(!$parser)
return false;
xml\_parser\_set\_option($parser, XML\_OPTION\_TARGET\_ENCODING, 'UTF-8');
xml\_parser\_set\_option($parser, XML\_OPTION\_CASE\_FOLDING, 0);
xml\_parser\_set\_option($parser, XML\_OPTION\_SKIP\_WHITE, 1);
xml\_parse\_into\_struct($parser, trim($contents), $xml\_values);
xml\_parser\_free($parser);
if (!$xml\_values)
return array();
$xml\_array = array();
$last\_tag\_ar =& $xml\_array;
$parents = array();
$last\_counter\_in\_tag = array(1=\>0);
foreach ($xml\_values as $data)
{
switch($data['type'])
{
case 'open':
$last\_counter\_in\_tag[$data['level']+1] = 0;
$new\_tag = array('name' =\> $data['tag']);
if(isset($data['attributes']))
$new\_tag['attributes'] = $data['attributes'];
if(isset($data['value']) && trim($data['value']))
$new\_tag['value'] = trim($data['value']);
$last\_tag\_ar[$last\_counter\_in\_tag[$data['level']]] = $new\_tag;
$parents[$data['level']] =& $last\_tag\_ar;
$last\_tag\_ar =& $last\_tag\_ar[$last\_counter\_in\_tag[$data['level']]++];
break;
case 'complete':
$new\_tag = array('name' =\> $data['tag']);
if(isset($data['attributes']))
$new\_tag['attributes'] = $data['attributes'];
if(isset($data['value']) && trim($data['value']))
$new\_tag['value'] = trim($data['value']);
$last\_count = count($last\_tag\_ar)-1;
$last\_tag\_ar[$last\_counter\_in\_tag[$data['level']]++] = $new\_tag;
break;
case 'close':
$last\_tag\_ar =& $parents[$data['level']];
break;
default:
break;
};
}
return $xml\_array;
}
$http\_request = new http\_request();
$xml = $http\_request-\>body();
$result = my\_xml2array($xml);
// Uncomment this and you can see in Lua the xml array created by the php funcion "my\_xml2array"
// that way you can find easier the nodes in the array
//print\_r ($result);
$numListas = count($result[0])-1;
for($j = 0; $j \< $numListas; $j++)
{
$idLista = $result[0][$j]["attributes"]["idLista"];
$numInvitados = count($result[0][$j]) - 2;
$i = 0;
while ($i \< $numInvitados)
{
$id = $result[0][$j][$i]["attributes"]["id"];
$didcome = $result[0][$j][$i]["attributes"]["didcome"];
if($id == "0")
{
$tipo = $result[0][$j][$i]["attributes"]["tipo"];
$zone = $result[0][$j][$i]["attributes"]["zone"];
$region = $result[0][$j][$i]["attributes"]["region"];
$name = $result[0][$j][$i][0]["value"];
$surname = $result[0][$j][$i][1]["value"];
$observation = utf8\_decode($result[0][$j][$i][2]["value"]);
$query = "INSERT INTO invitados (name, surname, observation, region, zone, idType, idLista, didcome) VALUES('".$name."', '".$surname."', '".$observation."','".$region."','".$zone."','".$tipo."','".$idLista."','".$didcome."' ) ";
}else
{
$query = "UPDATE invitados SET didcome = ".$didcome." WHERE invitados.id = ".$id;
}
mysql\_query($query) or die (mysql\_error());
++$i;
}
}
mysql\_close($cnx);
echo "1"; // I use this echo to check in Lua if everything went ok
?\>
[/blockcode]
Hope it can help someone. If you see anything wrong in the way I did something, please tell me.
seb [import]uid: 29594 topic\_id: 7255 reply\_id: 25917[/import]