John Dawson
John Dawson

Reputation: 465

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://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl";
// 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://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl";
// 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://scientific.thomsonreuters.com/schema/wok5.4/public/FullRecord");
$xml->registerXPathNamespace("ns2", "http://woksearch.v3.wokmws.thomsonreuters.com");
$xml->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");

// 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://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://auth.cxf.wokmws.thomsonreuters.com"><SOAP-ENV:Body><ns1:authenticate/></SOAP-ENV:Body></SOAP-ENV:Envelope>


Response: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:authenticateResponse xmlns:ns2="http://auth.cxf.wokmws.thomsonreuters.com"><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://scientific.thomsonreuters.com/schema/wok5.4/public/FullRecord">
        <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://scientific.thomsonreuters.com/schema/wok5.4/public/FullRecord">
    <REC r_id_disclaimer="Research " ... etc ... fier></identifiers></cluster_related></dynamic_data></REC>
    </records>
)

EXTRACTED STRING:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:searchResponse xmlns:ns2="http://woksearch.v3.wokmws.thomsonreuters.com">
            <return>
                <queryId>1</queryId>
                <recordsFound>3673</recordsFound>
                <recordsSearched>38835281</recordsSearched>
                <records>&lt;records xmlns="http://scientific.thomsonreuters.com/schema/wok5.4/public/FullRecord">
                &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.

Upvotes: 0

Views: 2120

Answers (1)

John Dawson
John Dawson

Reputation: 465

Figured it out. I was trying to turn the data returned in the response from the Soap Client object whereas I should have been trying to extract the data from the stdClass object that was returned as $search_response from doing $search_client->search($search_array). Turning the records property of this object into a SimpleXMLElement has provided me with the XML data in a coherent fashion.

I did this by:

$xml = new SimpleXMLElement($search_response->return->records);

as the attribute of the records property is a String that represents XML data.

Upvotes: 1

Related Questions