Reading XML response

Carl Perry
  • Carl Perry

    Carl Perry - 2008-04-13


    I am using the following code to make a request to a server:

    $wsdlURL = '';
    $soap = new soapclient($wsdlURL, 'wsdl');

    $username = 'NotMyRealOne';
    $password = 'FakePass';

    $params = array(
            "username" => $username,
            "password" => $password

        $result = $soap->call('Login',$params);
        if ($error = $soap->getError()) die($error);
    echo '<h2>Response</h2><pre>' . htmlspecialchars($soap->response, ENT_QUOTES) . '</pre>';

    Only problem is when I get the response I don't know how to convert the XML data into a readbale array or variable, this is the response I get:

    HTTP/1.1 200 OK
    Connection: close
    Date: Sun, 13 Apr 2008 10:56:51 GMT
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    X-AspNet-Version: 2.0.50727
    Cache-Control: private, max-age=0
    Content-Type: text/xml; charset=utf-8
    Content-Length: 547

    <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="" xmlns:xsi="" xmlns:xsd=""><soap:Body><LoginResponse xmlns=""><LoginResult><IsAuthenticated>true</IsAuthenticated><AuthorizationCode>1</AuthorizationCode><SessionKey>532448db-46e2-496a-a8ad-ca2cb7eabc41</SessionKey><Expiration>2008-04-13T12:38:41.493</Expiration></LoginResult></LoginResponse></soap:Body></soap:Envelope>

    I need to be able to read the <SessionKey> but am unsure of how to do this, can anyone help?

    Thank you for any help given,

    Kind Regards,

    • Carl Perry

      Carl Perry - 2008-04-13

      Never mind, I solved it

      I used the following code to access the element I needed

      $SessionKey = $result['LoginResult']['SessionKey'];

      Its quite simple really, I didn't realise that the array was infact XML elements, maybe this should be in the documentation as there seems to be alot of people with the same sort of problem.

      I used a foreach loop to find out the $key names in order to access them:

      $result = $soap->call('Login',$params);
      if ($error = $soap->getError()) die($error);

      foreach($result as $key => $value) {
         echo "key: $key , value: $value <br>";

  • Anonymous

    Anonymous - 2011-02-17

    An array with de data in your XML is already provided, use $result instead of $soap->response in your code.

  • Anonymous

    Anonymous - 2011-02-17

    Other detail, When you do:
    $soap = new soapclient($wsdlURL, 'wsdl');

    actually you are not indicating to use 'wsdl' but true, which means use wsdl, the definition is

    nusoap_client  (mixed $endpoint, , , , , , integer $timeout, , )

  • Anonymous

    Anonymous - 2011-02-17

    Finally, I could not retrieve the XML contents of the response by using SOAP, but the service was also available in GET and POST (.NET service  with the 3 protocols enabled), so I changed to GET (using CURL) and voilá, got the XML response data with no problem.

    Just in case it could work for you too.

  • Jax

    Jax - 2011-09-21

    Hey outasync ,

    thanks a lot… your reply helped me today!!! gr8 job!!


Log in to post a comment.