2011-08-24 100 views
0

我有一些簡單的SOAP客戶端從WSDL獲取數據並顯示它。在PHP中處理Web服務錯誤

<?php 
    //Data, connection, auth 
    $dataFromTheForm = $_POST['fieldName']; // request data from the form 
    $soapUrl = "https://connecting.website.com/soap.asmx?op=DoSomething"; // asmx URL of WSDL 
    $soapUser = "username"; // username 
    $soapPassword = "password; // password 

    // xml post structure 

    $xml_post_string = '<?xml version="1.0" encoding="utf-8"?> 
         <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
          <soap:Body> 
          <GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns value to be set to your's WSDL URL 
           <PRICE>'.$dataFromTheForm.'</PRICE> // data from the form, e.g. some ID number 
          </GetItemPrice > 
          </soap:Body> 
         </soap:Envelope>'; 

     $headers = array(
        "Content-type: text/xml;charset=\"utf-8\"", 
        "Accept: text/xml", 
        "Cache-Control: no-cache", 
        "Pragma: no-cache", 
        "SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice", // your op URL 
        "Content-length: ".strlen($xml_post_string), 
       ); 

     $url = $soapUrl; 

     // PHP cURL for https connection with auth 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc 
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $soapUrl); // asmx URL of WSDL - declared at the top of the doc 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

     // converting 
     $response = curl_exec($ch); 
     curl_close($ch); 

     // converting 
     $response1 = str_replace("<soap:Body>","",$response); 
     $response2 = str_replace("</soap:Body>","",$response1); 

     // convertingc to XML 
     $parser = simplexml_load_string($response2);   



?> 

一切工作正常 - 當WSDL工作正常。

但是,當WSDL失敗(運行時錯誤在服務器上),我的網站是投擲數百警告所有的線152是:

$parser = simplexml_load_string($response2);

如何檢測WSDL已返回無效的XML( HTML錯誤味精)並顯示簡單的錯誤味精在這個基地?

回答

2

檢查the documentation page.

提示 使用libxml_use_internal_errors尖端(),以抑制所有XML錯誤,libxml_get_errors(),以在他們之後迭代。

Here are some examples.


UPDATE:

對於這種情況的例子就是看起來像:

libxml_use_internal_errors(true); //enable error handling 
$parser = simplexml_load_string($output2); 

if(!$parser){ // if $parser is not valid XML response  
    echo '<p>Sorry. This service is currently unavailable. Please try again later.</p>'; 
} else { 
    //if $parser is valid XML response, do something 
} 
+0

你可以提供一些使用示例,請? – Iladarsda

+0

查看我添加的鏈接。 –

1

只需檢查標題(ContentType)或簡單地檢查$ response2是否以<?xml開頭。