1

I'm using this code to get an xml file :

<?php
$username ="XXXX";
$password = "XXXX";
$url = 'XXXX';

$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("$username:$password")
    )
));

$stringXML = file_get_contents($url, false, $context);
print_r($stringXML);
$stringXML = simplexml_load_string($stringXML);
echo "<br>";
print_r($stringXML);

My XML looks like:

<ns:getInfoResponse xmlns:ns="xxxx"> <ns:return> <result> <entry I_Personne="2291568592"> <loginGraceRemaining>10</loginGraceRemaining> <loginTime>20150827195311Z</loginTime> <loginDisabled>TRUE</loginDisabled> <isValidated>true</isValidated> <passwordExpirationTime>20160223195311Z</passwordExpirationTime> <mail_aai>xxxx.xxxx@xxxx.ch</mail_aai> </entry> </result> </ns:return> </ns:getInfoResponse> 

My second print_r is returning this : SimpleXMLElement Object ( )

Why is it empty ?

Jan
  • 42,290
  • 8
  • 54
  • 79
qwertzuiop
  • 685
  • 2
  • 10
  • 24
  • Check `$stringXML` is in a format of xml or not!! – Saty Jan 11 '16 at 13:34
  • Yeah, I see that but the url display xml code in my browser. What am I doing wrong ? – qwertzuiop Jan 11 '16 at 13:37
  • Here's what I get in my browser: This XML file does not appear to have any style information associated with it. The document tree is shown below. 10 20150827195311Z TRUE true 20160223195311Z xxxx.xxxx@xxxx.ch – qwertzuiop Jan 11 '16 at 13:38
  • Use [this](http://www.xmlvalidation.com/) to check xml is valid or not – Saty Jan 11 '16 at 13:41
  • 4
    Answer: it is not empty, you simply cannot see it with `print_r()`. Additionally, there are namespaces to consider. All of this can be found here on SO. – Jan Jan 11 '16 at 13:41
  • @Saty I checked my XML, no error... – qwertzuiop Jan 11 '16 at 13:45
  • Possible duplicate of [PHP namespace simplexml problems](http://stackoverflow.com/questions/2098170/php-namespace-simplexml-problems) – Jan Jan 11 '16 at 13:50
  • 1
    `echo $stringXML->asXML();`to see the `SimpleXml` object – michi Jan 11 '16 at 14:58

1 Answers1

1

The answer to your question is strictly that you cannot get a useful response with print_r on the SimpleXML object.

The more useful answer would be that you have to consider the namespace and the children of that namespace.

Since you stripped the namespace URL from the document, I'll replace the namespace with the URL http://example.org/namespace/

To get the loginTime for the entry, you could then do something similar to the following:

<?php
$stringXML = '<ns:getInfoResponse xmlns:ns="http://example.org/namespace/"> <ns:return> <result> <entry I_Personne="2291568592"> <loginGraceRemaining>10</loginGraceRemaining> <loginTime>20150827195311Z</loginTime> <loginDisabled>TRUE</loginDisabled> <isValidated>true</isValidated> <passwordExpirationTime>20160223195311Z</passwordExpirationTime> <mail_aai>xxxx.xxxx@xxxx.ch</mail_aai> </entry> </result> </ns:return> </ns:getInfoResponse>';

$xml = simplexml_load_string($stringXML);
$children = $xml->children("http://example.org/namespace/"); //loading the correct namespace and getting the children for it
echo (string)$children->{"return"}->children()[0]->children()[0]->loginTime;

Please note that there might be better ways of going to the right path, especially if you have a more complex document.

Repox
  • 15,015
  • 8
  • 54
  • 79