2014-08-28 148 views
0

我創建了一個站點,用戶可以輸入某些搜索參數,然後將這些參數輸入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>&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 () 

我感興趣的數據是在__last_response屬性Soap Client對象之內。包含在標籤中的數據是一條記錄,其中有許多其他數據字段(<title>,address,citations等)。

下的可變$string差不多的作品,但而SOAP信封顯示正確,它包圍XML數據與&lt;更換所有<標籤法從這裏的數據。它僅用於打開標籤並僅用於第二個<records>標籤內的元素。

正如你可以看到當我試圖把它變成SimpleXMLElement它返回一個空的對象,並試圖返回$result中的數據使用XPath返回一個空的數組。

有沒有人有任何想法如何從SOAP響應中獲得所需的數據?

謝謝你的幫助。

回答

1

想通了。我試圖將來自Soap Client對象的響應中返回的數據轉化爲數據,而我應該試圖從stdClass對象中提取數據,該對象從$search_client->search($search_array)$search_response的形式返回。將此對象的records屬性轉換爲SimpleXMLElement以一致的方式向我提供了XML數據。

我這樣做的:

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

records屬性的屬性是表示XML數據的字符串。