title: Can't create SimpleXMLElement from SOAP response of PHP Soap Client object
body: <p>I have created a site where a user can enter certain search parameters which are then fed into an XML SOAP Request in PHP to retrieve relevant data from a Web Service.</p>
  
  <p>I am retrieving this data but am having trouble extracting it from the Soap Client object it is returned in. Here is my PHP code:</p>
  
  <pre><code>// set WSDL for authentication and create new SOAP client
  $auth_url  = "http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl";
  // array options are temporary and used to track request &amp; response data in printout below
  $auth_client = @new SoapClient($auth_url, array(
                   "trace" =&gt; 1,
                   "exceptions" =&gt; 0));
  // run 'authenticate' method and store as variable
  $auth_response = $auth_client-&gt;authenticate();
  
  // set WSDL for search and create new SOAP client
  $search_url = "http://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl";
  // array options are temporary and used to track request &amp; response data in printout below
  $search_client = @new SoapClient($search_url, array(
                   "trace" =&gt; 1,
                   "exceptions" =&gt; 0));
  // call 'setCookie' method on '$search_client' storing SID (Session ID) as the response (value) given from the 'authenticate' method
  $search_client-&gt;__setCookie('SID',$auth_response-&gt;return);
  
  // print details of XML request and response data for Authentication exchange
  print "&lt;pre&gt;\n";
  print "&lt;br /&gt;\n Request : ".htmlspecialchars($auth_client-&gt;__getLastRequest());
  print "&lt;br /&gt;\n Response: ".htmlspecialchars($auth_client-&gt;__getLastResponse());
  print "&lt;/pre&gt;";
  
  // pass in relevant parameters for search, $_POSTed params from user entry
  $search_array = array(
      'queryParameters' =&gt; array(
          'databaseId' =&gt; 'WOS',
          'userQuery' =&gt; $_POST["type"].'='.$_POST["category"],
          'editions' =&gt; array('collection' =&gt; 'WOS', 'edition' =&gt; 'SCI'),
          'queryLanguage' =&gt; 'en'
      ),
      'retrieveParameters' =&gt; array(
          'count' =&gt; '5',
          'sortField' =&gt; array(
              array('name' =&gt; $_POST["sort"], 'sort' =&gt; 'D')
          ),
          'firstRecord' =&gt; '1'
      )
  );
  
  // try to store as a variable the 'search' method on the '$search_array' called on the SOAP client with associated SID 
  try {
      $search_response = $search_client-&gt;search($search_array);
  } catch (Exception $e) {  
      echo $e-&gt;getMessage(); 
  };
  
  echo "&lt;/br&gt;SEARCH_RESPONSE: &lt;/br&gt;";
  print "&lt;pre&gt;\n";
  print_r($search_response);
  print "&lt;/pre&gt;";
  
  echo "&lt;/br&gt;SEARCH_CLIENT: &lt;/br&gt;";
  print "&lt;pre&gt;\n";
  print_r($search_client);
  print "&lt;/pre&gt;";
  
  // extract the response from the Soap Client object
  $string = htmlspecialchars($search_client-&gt;__getLastResponse());
  
  echo "&lt;/br&gt;&lt;/br&gt;EXTRACTED STRING: &lt;/br&gt;&lt;/br&gt;";
  print "&lt;pre&gt;\n";
  print_r ($string);
  print "&lt;/pre&gt;";
  
  // turn Soap Client object into SimpleXMLElement
  $xml = new SimpleXMLElement($search_client-&gt;__getLastResponse());
  
  // register the namespaces
  $xml-&gt;registerXPathNamespace("ns1", "http://scientific.thomsonreuters.com/schema/wok5.4/public/FullRecord");
  $xml-&gt;registerXPathNamespace("ns2", "http://woksearch.v3.wokmws.thomsonreuters.com");
  $xml-&gt;registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
  
  // initiate the xpath
  $xpath = "/soap:Envelope/soap:Body/ns2:searchResponse/return/records/ns1:records";
  
  $result = $xml-&gt;xpath($xpath);
  
  print_r($xml);
  print_r($result);
  </code></pre>
  
  <p>and here is what is displayed onto the 'result' web page when the search service is processed:</p>
  
  <pre><code>Request : &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://auth.cxf.wokmws.thomsonreuters.com"&gt;&lt;SOAP-ENV:Body&gt;&lt;ns1:authenticate/&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;
  
  
  Response: &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soap:Body&gt;&lt;ns2:authenticateResponse xmlns:ns2="http://auth.cxf.wokmws.thomsonreuters.com"&gt;&lt;return&gt;W9tJXpyEV4xrkbr6t19&lt;/return&gt;&lt;/ns2:authenticateResponse&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;
  
  SEARCH_RESPONSE:
  
  stdClass Object
  (
      [return] =&gt; stdClass Object
      (
          [queryId] =&gt; 1
          [recordsFound] =&gt; 3673
          [recordsSearched] =&gt; 38835281
          [records] =&gt;
          &lt;records xmlns="http://scientific.thomsonreuters.com/schema/wok5.4/public/FullRecord"&gt;
          &lt;REC r_id_disclaimer="Rese "... etc ... fier&gt;&lt;/identifiers&gt;&lt;/cluster_related&gt;&lt;/dynamic_data&gt;&lt;/REC&gt;
          &lt;/records&gt;
      )
  )
  
  SEARCH_CLIENT:
  
  SoapClient Object
  (
      [trace] =&gt; 1
      [_exceptions] =&gt; 
      [_soap_version] =&gt; 1
      [sdl] =&gt; Resource id #9
      [_cookies] =&gt; Array
      (
          [SID] =&gt; Array
              (
                  [0] =&gt; W9tJXpyEV4xrkbr6t19
              )
  
      )
  
      [__last_request] =&gt; WOSTS=botanyWOSSCIen15RSD
      [httpsocket] =&gt; Resource id #10
      [_use_proxy] =&gt; 0
      [httpurl] =&gt; Resource id #11
      [__last_request_headers] =&gt; POST /esti/wokmws/ws/WokSearch HTTP/1.1
      Host: search.webofknowledge.com
      Connection: Keep-Alive
      User-Agent: PHP-SOAP/5.5.9
      Content-Type: text/xml; charset=utf-8
      SOAPAction: ""
      Content-Length: 588
      Cookie: SID=W9tJXpyEV4xrkbr6t19;
  
      [__last_response_headers] =&gt; HTTP/1.1 200 OK
      Server: Apache-Coyote/1.1
      Content-Type: text/xml;charset=UTF-8
      Transfer-Encoding: chunked
      Date: Thu, 28 Aug 2014 10:23:29 GMT
  
      [__last_response] =&gt; 1367338835281&lt;records xmlns="http://scientific.thomsonreuters.com/schema/wok5.4/public/FullRecord"&gt;
      &lt;REC r_id_disclaimer="Research " ... etc ... fier&gt;&lt;/identifiers&gt;&lt;/cluster_related&gt;&lt;/dynamic_data&gt;&lt;/REC&gt;
      &lt;/records&gt;
  )
  
  EXTRACTED STRING:
  
  &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
      &lt;soap:Body&gt;
          &lt;ns2:searchResponse xmlns:ns2="http://woksearch.v3.wokmws.thomsonreuters.com"&gt;
              &lt;return&gt;
                  &lt;queryId&gt;1&lt;/queryId&gt;
                  &lt;recordsFound&gt;3673&lt;/recordsFound&gt;
                  &lt;recordsSearched&gt;38835281&lt;/recordsSearched&gt;
                  &lt;records&gt;&amp;lt;records xmlns="http://scientific.thomsonreuters.com/schema/wok5.4/public/FullRecord"&gt;
                  &amp;lt;REC r_id_disclaimer="ResearcherID data prov " ... etc ... ifiers&gt;&amp;lt;/cluster_related&gt;&amp;lt;/dynamic_data&gt;&amp;lt;/REC&gt;
                  &amp;lt;/records&gt;&lt;/records&gt;
              &lt;/return&gt;
          &lt;/ns2:searchResponse&gt;
      &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  
  SimpleXMLElement Object ( ) Array ( )
  </code></pre>
  
  <p>The data I am interested in is within the <code>Soap Client</code> object in the <code>__last_response</code> property. The data enclosed in a <code>&lt;REC&gt;</code> tag is one record from which there are many other data fields (<code>&lt;title&gt;</code>, <code>address</code>, <code>citations</code> etc.).</p>
  
  <p>Extracting the data from here under the variable <code>$string</code> almost works but whereas the SOAP envelope appears correctly, the XML data it encloses is replacing all <code>&lt;</code> tags with <code>&amp;lt;</code>. It only does this for opening tags and only for elements within the second <code>&lt;records&gt;</code> tag.</p>
  
  <p>As you can see when I try to turn it into a <code>SimpleXMLElement</code> it returns an empty object and trying to return the data in <code>$result</code> using XPath returns an empty array.</p>
  
  <p>Does anyone have any ideas how I can get the data I need from this SOAP response, please?</p>
  
  <p>Thank you for your help.</p>