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>[email protected]</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

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>[email protected]</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