处理 PHP 中的 Web 服务错误

发布于 2024-12-01 12:15:42 字数 2794 浏览 0 评论 0原文

我有一些简单的 SOAP 客户端来从 WSDL 获取数据并显示它。

<?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 错误消息)并在此基础上显示简单的错误消息?

I have some simple SOAP client to get data from WSDL and display it.

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



?>

Everything works fine - when WSDL works fine.

But, when WSDL fails( Runtime Error on the server) my site is throwing hundreds of warnings all at line 152 which is:

$parser = simplexml_load_string($response2);

How to detect that WSDL have returned invalid XML (html error msg) and display simple error msg on this base?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Oo萌小芽oO 2024-12-08 12:15:42

查看文档页面上的提示。

提示
使用 libxml_use_internal_errors() 抑制所有 XML 错误,然后使用 libxml_get_errors() 迭代它们。

这里有一些示例。


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
}

Check the tip on the documentation page.

Tip
Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.

Here are some examples.


UPDATE:

For this case example would look like that:

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
}
挽容 2024-12-08 12:15:42

只需检查标头 (ContentType) 或更简单地检查 $response2(如果它以 开头)。

Simply by checking the header (ContentType) or simpler check the $response2 if it's starts with <?xml .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文