I’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]<?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’];
}
?>[/php]
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.