Jump to content

Help needed with XML to Array


mobifree

Recommended Posts

'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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

  • 7 months later...

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,

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.