无法从PHP SOAP SOAP客户端对象的SOAP响应中创建SimpleXMLElement
问题描述:
我创建了一个站点,用户可以输入某些搜索参数,然后将这些参数输入PHP中的XML SOAP请求以从Web Service检索相关数据。 。无法从PHP SOAP SOAP客户端对象的SOAP响应中创建SimpleXMLElement
我获取这个数据,但我有麻烦,从它在返回的SOAP客户端对象提取呢?这里是我的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><records xmlns="http://scientific.thomsonreuters.com/schema/wok5.4/public/FullRecord">
<REC r_id_disclaimer="ResearcherID data prov " ... etc ... ifiers></cluster_related></dynamic_data></REC>
</records></records>
</return>
</ns2:searchResponse>
</soap:Body>
</soap:Envelope>
SimpleXMLElement Object () Array ()
我感兴趣的数据是在__last_response
属性Soap Client
对象之内。包含在标签中的数据是一条记录,其中有许多其他数据字段(<title>
,address
,citations
等)。
下的可变$string
差不多的作品,但而SOAP信封显示正确,它包围XML数据与<
更换所有<
标签法从这里的数据。它仅用于打开标签并仅用于第二个<records>
标签内的元素。
正如你可以看到当我试图把它变成SimpleXMLElement
它返回一个空的对象,并试图返回$result
中的数据使用XPath返回一个空的数组。
有没有人有任何想法如何从SOAP响应中获得所需的数据?
谢谢你的帮助。
答
想通了。我试图将来自Soap Client
对象的响应中返回的数据转化为数据,而我应该试图从stdClass
对象中提取数据,该对象从$search_client->search($search_array)
以$search_response
的形式返回。将此对象的records
属性转换为SimpleXMLElement
以一致的方式向我提供了XML数据。
我这样做的:
$xml = new SimpleXMLElement($search_response->return->records);
为records
属性的属性是表示XML数据的字符串。