mobifree Posted May 13, 2011 Share Posted May 13, 2011 'm trying to write a script that will covert an XML response to an Array. The format of the xml response is the following (1 of 2 possibilities): if found <response> <imei>123123123123111</imei> <unlockcode>34343434</unlockcode> </response> if not found: <response> <imei>123123123123111</imei> <unlockcode>Not Found</unlockcode> </response> I've been able to create the xml files yes.xml and no.xml and using this script I found on the internet have been able to parse it. <?php function objectsIntoArray($arrObjData, $arrSkipIndices = array()) { $arrData = array(); // if input is object, convert into array if (is_object($arrObjData)) { $arrObjData = get_object_vars($arrObjData); } if (is_array($arrObjData)) { foreach ($arrObjData as $index => $value) { if (is_object($value) || is_array($value)) { $value = objectsIntoArray($value, $arrSkipIndices); // recursive call } if (in_array($index, $arrSkipIndices)) { continue; } $arrData[$index] = $value; } } return $arrData; } $xmlUrl = "no.xml"; // XML feed file/URL $xmlStr = file_get_contents($xmlUrl); $xmlObj = simplexml_load_string($xmlStr); $arrXml = objectsIntoArray($xmlObj); // print_r($arrXml); // print $arrXml['unlockcode']; if ($arrXml['unlockcode'] == "Not Found") { echo "Unable to find the unlock code"; } else { echo "Found the code! Here it is!"; echo $arrXml['unlockcode']; } ?> What I'm trying to do is create a file response.php that will parse the information sent from the server in the xml format described above into an array (instead of reading it from a local file). Your help is appreciated! Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/236291-help-needed-with-xml-to-array/ Share on other sites More sharing options...
jonsjava Posted May 13, 2011 Share Posted May 13, 2011 I would recommend you use SimpleXML for handling XML. Quote Link to comment https://forums.phpfreaks.com/topic/236291-help-needed-with-xml-to-array/#findComment-1214848 Share on other sites More sharing options...
mobifree Posted May 13, 2011 Author Share Posted May 13, 2011 Thanks for the response. Here's my question. Since there is no actual XML file that resides on my server (the xml information is a response from a 3rd party server) how will I handle the information? Will I have to create a script to first write a temp .xml file with the response then, use the SimpleXML script to parse this data into an array? Or is there a 1 shot process where the XML response from the 3rd party server gets parsed in the same script (without creating a temp .xml file everytime the 3rd party server sends a response)? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/236291-help-needed-with-xml-to-array/#findComment-1215113 Share on other sites More sharing options...
codebyren Posted May 13, 2011 Share Posted May 13, 2011 jonsjava has shown you the light. If you read the SimpleXML docs, you will see there is a way to load XML from a string directly: <?php $string = "<response><imei>123123123123111</imei><unlockcode>34343434</unlockcode></response>"; $xmlobj = simplexml_load_string($string); // Play with objects $imei = $xmlobj->imei; $unlockcode = $xmlobj->unlockcode; echo "The imei is $imei and the unlock code is $unlockcode <br />"; // or play with arrays $xml_array = (array) $xmlobj; echo "The imei is {$xml_array['imei']} and the unlock code is {$xml_array['unlockcode']}"; ?> That should get you going... Quote Link to comment https://forums.phpfreaks.com/topic/236291-help-needed-with-xml-to-array/#findComment-1215174 Share on other sites More sharing options...
mobifree Posted January 7, 2012 Author Share Posted January 7, 2012 Sorry to bring up an incredibly old thread. I figured since the subject is follow up on the original topic, why waste a new thread. <? include(".inc/config.php"); $xml = file_get_contents("php://input"); $xmlobj = simplexml_load_string($xml); // Play with objects $imei = $xmlobj->imei; $code = $xmlobj->unlockcode; $message = " $xml IMEI: $imei Code: $code "; mailt($to, $subject, $message, "From: $from"); ?> This is the e-mail I get. Where $code and $imei are blank. data= <response> <imei>123456789123456</imei> <unlockcode>12345678</unlockcode> </response> IMEI: Code: I'm just e-mailing myself so that I can see what exactly is being outputted. Ideally I just want to have 2 usable variables ($imei and $code) from the xml response from the API server. Help is appreciated. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/236291-help-needed-with-xml-to-array/#findComment-1305392 Share on other sites More sharing options...
devWhiz Posted January 8, 2012 Share Posted January 8, 2012 Can you post the full xml stream? Quote Link to comment https://forums.phpfreaks.com/topic/236291-help-needed-with-xml-to-array/#findComment-1305428 Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2012 Share Posted January 8, 2012 Your input string contains - data=. That is not xml and makes your xml source invalid. You need to have php's error_reporting set to E_ALL and display_errors set to ON or log_errors set to ON so that all the php detected errors will be reported and displayed/logged. You would have been getting the following warnings and notices - Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in your_file.php on line x Warning: simplexml_load_string() [function.simplexml-load-string]: data= in your_file.php on line x Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in your_file.php on line x Notice: Trying to get property of non-object in your_file.php on line y Notice: Trying to get property of non-object in your_file.php on line z Quote Link to comment https://forums.phpfreaks.com/topic/236291-help-needed-with-xml-to-array/#findComment-1305432 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.