Hi Peeps 🙂
I'm really stuck on this code, which is sent to my server as a xml doc.
I'm trying to extract the elements from it, and put them in to $vars so I can then enter them in to a database.
Its really doing my head in now, and I was wondering if some 'fresh eyes' could take a look.
this is my index.php file where the data arrives as an xml doc.
<?PHP
require_once("XMLAssocArray.class");
$parser = new XMLAssocArray();
$arr = $parser->xml2array($xml);
$msisdn = $arr['msisdn'];
$subref = $arr['submission_ref'];
$status = $arr['status'];
$reason = $arr['reason']; // this should be 4 for success.
// log the inputted xml to a text file
$your_data = $subref.", Hello MSISDN=$msisdn, STATUS=$status, REASON=$reason, FULL XML=$xml";
//$your_data = $xml;
$your_data = $your_data . "\n";
$fp = fopen("log/log.txt", "a");
fwrite($fp, $your_data);
fclose($fp);
// end logging url to test file
//echo "<br>$subref<br>$msisdn<br>$status<br>$reason";
?>
This is the .class file that is supposed to extract the xml elements to the $vars.
<?PHP
class XMLAssocArray {
var $text;
var $arrays, $keys, $node_flag, $depth, $xml_parser;
/*Converts an array to an xml string*/
function array2xml($array) {
//global $text;
$this->text="<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><array>";
$this->text.= $this->array_transform($array);
$this->text .="</array>";
return $this->text;
}
function array_transform($array){
//global $array_text;
foreach($array as $key => $value){
if(!is_array($value)){
$this->text .= "<$key>$value</$key>";
} else {
$this->text.="<$key>";
$this->array_transform($value);
$this->text.="</$key>";
}
}
return $array_text;
}
/*Transform an XML string to associative array "XML Parser Functions"*/
function xml2array($xml){
$this->depth=-1;
$this->xml_parser = xml_parser_create();
xml_set_object($this->xml_parser, $this);
xml_parser_set_option ($this->xml_parser,XML_OPTION_CASE_FOLDING,0);//Don't put tags uppercase
xml_set_element_handler($this->xml_parser, "startElement", "endElement");
xml_set_character_data_handler($this->xml_parser,"characterData");
xml_parse($this->xml_parser,$xml,true);
xml_parser_free($this->xml_parser);
// return $this->arrays[0];
return $this->arrays[1];
}
function startElement($parser, $name, $attrs)
{
$this->keys[]=$name; //We add a key
$this->node_flag=1;
$this->depth++;
}
function characterData($parser,$data)
{
$key=end($this->keys);
$this->arrays[$this->depth][$key]=$data;
$this->node_flag=0; //So that we don't add as an array, but as an element
}
function endElement($parser, $name)
{
$key=array_pop($this->keys);
//If $node_flag==1 we add as an array, if not, as an element
if($this->node_flag==1){
$this->arrays[$this->depth][$key]=$this->arrays[$this->depth+1];
unset($this->arrays[$this->depth+1]);
}
$this->node_flag=1;
$this->depth--;
}
}//End of the class
?>
When the data comes in to my server, the resulting log.txt file looks like this...
, Hello MSISDN=, STATUS=, REASON=, FULL XML=<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<itagg_delivery_receipt>
<version>1.0</version>
<msisdn>447921614202</msisdn>
<submission_ref>9b6bc3cf8caabef579d4251bd9ca264e</submission_ref>
<status>acked</status>
<reason></reason>
<gmt_timestamp>200601281447</gmt_timestamp>
</itagg_delivery_receipt>
When what it should look like is this....
9b6bc3cf8caabef579d4251bd9ca264e, Hello MSISDN=447921614202, STATUS=acked, REASON=, FULL XML=<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<itagg_delivery_receipt>
<version>1.0</version>
<msisdn>447921614202</msisdn>
<submission_ref>9b6bc3cf8caabef579d4251bd9ca264e</submission_ref>
<status>acked</status>
<reason></reason>
<gmt_timestamp>200601281447</gmt_timestamp>
</itagg_delivery_receipt>
Anybody know whats happening? :queasy: