Can't create SimpleXMLElement from SOAP response of PHP Soap Client object



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.


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:



// set WSDL for authentication and create new SOAP client
$auth_url = "http://ift.tt/1tZgd5O";
// array options are temporary and used to track request & response data in printout below
$auth_client = @new SoapClient($auth_url, array(
"trace" => 1,
"exceptions" => 0));
// run 'authenticate' method and store as variable
$auth_response = $auth_client->authenticate();

// set WSDL for search and create new SOAP client
$search_url = "http://ift.tt/1qCUUrs";
// array options are temporary and used to track request & response data in printout below
$search_client = @new SoapClient($search_url, array(
"trace" => 1,
"exceptions" => 0));
// call 'setCookie' method on '$search_client' storing SID (Session ID) as the response (value) given from the 'authenticate' method
$search_client->__setCookie('SID',$auth_response->return);

// print details of XML request and response data for Authentication exchange
print "<pre>\n";
print "<br />\n Request : ".htmlspecialchars($auth_client->__getLastRequest());
print "<br />\n Response: ".htmlspecialchars($auth_client->__getLastResponse());
print "</pre>";

// pass in relevant parameters for search, $_POSTed params from user entry
$search_array = array(
'queryParameters' => array(
'databaseId' => 'WOS',
'userQuery' => $_POST["type"].'='.$_POST["category"],
'editions' => array('collection' => 'WOS', 'edition' => 'SCI'),
'queryLanguage' => 'en'
),
'retrieveParameters' => array(
'count' => '5',
'sortField' => array(
array('name' => $_POST["sort"], 'sort' => 'D')
),
'firstRecord' => '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->search($search_array);
} catch (Exception $e) {
echo $e->getMessage();
};

echo "</br>SEARCH_RESPONSE: </br>";
print "<pre>\n";
print_r($search_response);
print "</pre>";

echo "</br>SEARCH_CLIENT: </br>";
print "<pre>\n";
print_r($search_client);
print "</pre>";

// extract the response from the Soap Client object
$string = htmlspecialchars($search_client->__getLastResponse());

echo "</br></br>EXTRACTED STRING: </br></br>";
print "<pre>\n";
print_r ($string);
print "</pre>";

// turn Soap Client object into SimpleXMLElement
$xml = new SimpleXMLElement($search_client->__getLastResponse());

// register the namespaces
$xml->registerXPathNamespace("ns1", "http://ift.tt/1s4y6z8");
$xml->registerXPathNamespace("ns2", "http://ift.tt/1xxc4tr");
$xml->registerXPathNamespace("soap", "http://ift.tt/sVJIaE");

// initiate the xpath
$xpath = "/soap:Envelope/soap:Body/ns2:searchResponse/return/records/ns1:records";

$result = $xml->xpath($xpath);

print_r($xml);
print_r($result);


and here is what is displayed onto the 'result' web page when the search service is processed:



Request : <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://ift.tt/sVJIaE" xmlns:ns1="http://ift.tt/1tZgd5W"><SOAP-ENV:Body><ns1:authenticate/></SOAP-ENV:Body></SOAP-ENV:Envelope>


Response: <soap:Envelope xmlns:soap="http://ift.tt/sVJIaE"><soap:Body><ns2:authenticateResponse xmlns:ns2="http://ift.tt/1tZgd5W"><return>W9tJXpyEV4xrkbr6t19</return></ns2:authenticateResponse></soap:Body></soap:Envelope>

SEARCH_RESPONSE:

stdClass Object
(
[return] => stdClass Object
(
[queryId] => 1
[recordsFound] => 3673
[recordsSearched] => 38835281
[records] =>
<records xmlns="http://ift.tt/1s4y6z8">
<REC r_id_disclaimer="Rese "... etc ... fier></identifiers></cluster_related></dynamic_data></REC>
</records>
)
)

SEARCH_CLIENT:

SoapClient Object
(
[trace] => 1
[_exceptions] =>
[_soap_version] => 1
[sdl] => Resource id #9
[_cookies] => Array
(
[SID] => Array
(
[0] => W9tJXpyEV4xrkbr6t19
)

)

[__last_request] => WOSTS=botanyWOSSCIen15RSD
[httpsocket] => Resource id #10
[_use_proxy] => 0
[httpurl] => Resource id #11
[__last_request_headers] => 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] => 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] => 1367338835281<records xmlns="http://ift.tt/1s4y6z8">
<REC r_id_disclaimer="Research " ... etc ... fier></identifiers></cluster_related></dynamic_data></REC>
</records>
)

EXTRACTED STRING:

<soap:Envelope xmlns:soap="http://ift.tt/sVJIaE">
<soap:Body>
<ns2:searchResponse xmlns:ns2="http://ift.tt/1xxc4tr">
<return>
<queryId>1</queryId>
<recordsFound>3673</recordsFound>
<recordsSearched>38835281</recordsSearched>
<records>&lt;records xmlns="http://ift.tt/1s4y6z8">
&lt;REC r_id_disclaimer="ResearcherID data prov " ... etc ... ifiers>&lt;/cluster_related>&lt;/dynamic_data>&lt;/REC>
&lt;/records></records>
</return>
</ns2:searchResponse>
</soap:Body>
</soap:Envelope>

SimpleXMLElement Object ( ) Array ( )


The data I am interested in is within the Soap Client object in the __last_response property. The data enclosed in a <REC> tag is one record from which there are many other data fields (<title>, address, citations etc.).


Extracting the data from here under the variable $string almost works but whereas the SOAP envelope appears correctly, the XML data it encloses is replacing all < tags with &lt;. It only does this for opening tags and only for elements within the second <records> tag.


As you can see when I try to turn it into a SimpleXMLElement it returns an empty object and trying to return the data in $result using XPath returns an empty array.


Does anyone have any ideas how I can get the data I need from this SOAP response, please?


Thank you for your help.


No comments:

Post a Comment