在 PHP 中处理 Web 服务错误 [英] Handling web service errors within PHP

查看:21
本文介绍了在 PHP 中处理 Web 服务错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



?>

一切正常 - 当 WSDL 正常工作时.

Everything works fine - when WSDL works fine.

但是,当 WSDL 失败(服务器上的运行时错误)时,我的站点会在第 152 行抛出数百个警告,即:

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

如何检测 WSDL 返回了无效的 XML(html 错误消息)并在此基础上显示简单的错误消息?

推荐答案

查看 文档页面.

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

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

以下是一些示例.


更新:

对于这种情况,示例如下所示:

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
}

这篇关于在 PHP 中处理 Web 服务错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆