0

i am getting this xml as a reponse from server

<?xml version='1.0' encoding='utf-8' standalone='no'?><REGISTRATIONRESPONSE><STATUSCODE>20</STATUSCODE><STATUS>1234</STATUS></REGISTRATIONRESPONSE>

i want to retrieve values from this xml if i directly use this string like this i am able to get values separately.

  $xmlString ="<?xml version='1.0' encoding='utf-8' standalone='no'?>
<REGISTRATIONRESPONSE>
<STATUSCODE>20</STATUSCODE>
<STATUS>1234</STATUS>
</REGISTRATIONRESPONSE>";
 $xml = simplexml_load_string($xmlString);
 $array = (array) $xml;
 var_dump($array);
 var_dump($array['STATUSCODE']);
 var_dump($array['STATUS']);

Result Is:

 array(2) {
 ["STATUSCODE"]=>
 string(2) "20"
 ["STATUS"]=>
 string(4) "1234"
  }
 string(2) "20"
 string(4) "1234"

but when i try directly take the response from api like this

   $result =curl_exec($ch);
   print_r($result); 
   echo '<pre>';
   $xml = simplexml_load_string($result);
   $xml = simplexml_load_string($result);
   $array = (array) $xml;
   var_dump($array);
   var_dump($array['STATUSCODE']);
   var_dump($array['STATUS']); 

result which is coming is like this:

   array(1) {
            [0]=>  string(147) "201234"
            }
            NULL
            NULL

I want to get result as i am getting in the 1st case but i can input manually like that every time i want to use variable instead of using xml as a string.

  • i tried the way given in that but the problem is if i use xml as a string i am getting output if i use xml in variable i am not able to get the output. – saksham mangwana May 08 '15 at 10:37
  • Please read about how to use **SimpleXMLElement** (that is what you create the variable of) within this guide: [Basic SimpleXML usage](https://php.net/manual/en/simplexml.examples-basic.php) - this should not only answer your question but should explain how the **SimpleXMLElement** can be used to access different data from the XML document not only those two element node values. – hakre May 08 '15 at 16:07
  • in all those examples and explanation xml is taken directly into a variable – saksham mangwana May 09 '15 at 09:13
  • Sure, like you do with the string. it's technically the same. – hakre May 09 '15 at 21:37

1 Answers1

0

if you are getting xml from url using curl you can read xml like below

 public function get_data_from_url()
    {

        $Url = "Xml Url";

        $ch = curl_init();

        // Now set some options (most are optional)

        // Set URL to download
        curl_setopt($ch, CURLOPT_URL, $Url);

        // Include header in result? (0 = yes, 1 = no)
        curl_setopt($ch, CURLOPT_HEADER, 0);

        // Should cURL return or print out the data? (true = return, false = print)
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Timeout in seconds
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        // Set Data Decompression
        curl_setopt($ch, CURLOPT_ENCODING, '');

        // Download the given URL, and return output
        $output = curl_exec($ch);

        // Close the cURL resource, and free system resources
        curl_close($ch);

        // Parse XML with SimpleXML
       if(!empty($output))
        {
        $livescore_data = new SimpleXMLElement(stripslashes($output));
        return $livescore_data;

        }

    }
Vivek Singh
  • 2,453
  • 1
  • 14
  • 27
  • i used the method suggested by you but result is coming like this `SimpleXMLElement Object ( [0] => 201234 ) ` but i want both the values separately 20 and 1234 they are both different i am not able to get them separately – saksham mangwana May 08 '15 at 11:56