无法从PHP Soap Client对象的SOAP响应创建SimpleXMLElement

时间:2014-08-28 11:22:11

标签: php xml web-services soap soap-client

我创建了一个网站,用户可以输入某些搜索参数,然后将这些参数输入PHP中的XML SOAP请求,以便从Web服务中检索相关数据。

我正在检索此数据,但是无法从返回的Soap Client对象中提取它。这是我的PHP代码:

// 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);

以下是处理搜索服务时在“结果”网页上显示的内容:

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 ( )

我感兴趣的数据位于Soap Client属性中的__last_response对象内。 <REC>标记中包含的数据是一条记录,其中有许多其他数据字段(<title>addresscitations等。)

在变量$string下从这里提取数据几乎可以正常工作,但是SOAP信封正确显示,它所包含的XML数据正在用<替换所有&lt;标签。它仅用于打开标记,仅用于第二个<records>标记内的元素。

正如您所看到的,当我尝试将其转换为SimpleXMLElement时,它会返回一个空对象并尝试使用XPath返回$result中的数据,返回一个空数组。

有没有人有任何想法如何从这个SOAP响应中获取我需要的数据呢?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

想出来。我试图转换来自Soap Client对象的响应中返回的数据,而我应该尝试从执行stdClass的{​​{1}}对象中提取数据$search_response 1}}。将此对象的$search_client->search($search_array)属性转换为records,以一致的方式为我提供了XML数据。

我是这样做的:

SimpleXMLElement

由于$xml = new SimpleXMLElement($search_response->return->records); 属性的属性是表示XML数据的String。