php - 无法从 Soap 响应中提取数据

标签 php soap xpath domxpath

这是我使用的代码:

$doc = // SOAP Response
            $xpath = new DOMXPath($doc);
            $xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
            $xpath->registerNamespace('api', 'http://127.0.0.1/Integrics/Enswitch/API');

// Response:
            if ($xpath->query('/soap:Envelope/soap:Body/api:get_cdrsResponse')->length < 1)
            {
                throw new EnswitchResponseFaultException();
            }

它一直抛出这个异常我做错了什么?

这是我得到的响应(Pastebin link):

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><get_cdrsResponse 
    xmlns="http://www.flatplanetphone.net/Integrics/Enswitch/API">
<s-gensym3>
<scustomer xsi:type="xsd:string">4458</scustomer>
<recording xsi:type="xsd:string" />
<outgroup_name xsi:type="xsd:string">abc</outgroup_name>
<bill_type xsi:type="xsd:string">prepaid</bill_type>
</s-gensym3>

最佳答案

简介

不确定你在哪里得到 $xpath->registerNamespace('api', 'http://127.0.0.1/Integrics/Enswitch/API'); 但不是有效的 命名空间 去获取所有你可以使用的有效命名空间

$sxe = new SimpleXMLElement($xml);
print_r($sxe->getDocNamespaces(true));

输出

Array
(
    [xsi] => http://www.w3.org/2001/XMLSchema-instance
    [soapenc] => http://schemas.xmlsoap.org/soap/encoding/
    [xsd] => http://www.w3.org/2001/XMLSchema
    [soap] => http://schemas.xmlsoap.org/soap/envelope/
    [] => http://www.flatplanetphone.net/Integrics/Enswitch/API
)

来自 pastbin 的 XML

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><get_cdrsResponse 
    xmlns="http://www.flatplanetphone.net/Integrics/Enswitch/API">
<s-gensym3>
<scustomer xsi:type="xsd:string">4458</scustomer>
<recording xsi:type="xsd:string" />
<outgroup_name xsi:type="xsd:string">abc</outgroup_name>
<bill_type xsi:type="xsd:string">prepaid</bill_type>
</s-gensym3>
</get_cdrsResponse>
</soap:Body>
</soap:Envelope>';

解决方案

$sxe = new SimpleXMLElement($xml);
$sxe->registerXPathNamespace("api", "http://www.flatplanetphone.net/Integrics/Enswitch/API");
$path = $sxe->xpath("//api:s-gensym3");
$info = array_shift($path);

if (!$info)
    throw new Exception("Invalid Response");

echo $info->scustomer, PHP_EOL;
echo $info->recording, PHP_EOL;
echo $info->outgroup_name, PHP_EOL;
echo $info->bill_type, PHP_EOL;

Output

4458

abc
prepaid

See Live DEMO

======================================

更新

======================================

当处理多个 child 时,你可以忽略命名空间并使用 SimpleXMLElement::children

例子

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <get_cdrsResponse xmlns="http://www.flatplanetphone.net/Integrics/Enswitch/API">
            <s-gensym3>
                <scustomer xsi:type="xsd:string">4458</scustomer>
                <recording xsi:type="xsd:string" />
                <outgroup_name xsi:type="xsd:string">abc</outgroup_name>
                <bill_type xsi:type="xsd:string">prepaid14</bill_type>
            </s-gensym3>
            <s-gensym5>
                <scustomer xsi:type="xsd:string">4458</scustomer>
                <recording xsi:type="xsd:string" />
                <outgroup_name xsi:type="xsd:string">abcd</outgroup_name>
                <bill_type xsi:type="xsd:string">prepaid</bill_type>
            </s-gensym5>
            <s-gensym7>
                <scustomer xsi:type="xsd:string">4458</scustomer>
                <recording xsi:type="xsd:string" />
                <outgroup_name xsi:type="xsd:string">abce</outgroup_name>
                <bill_type xsi:type="xsd:string">prepaid13</bill_type>
            </s-gensym7>
            <s-gensym11>
                <scustomer xsi:type="xsd:string">4458</scustomer>
                <recording xsi:type="xsd:string" />
                <outgroup_name xsi:type="xsd:string">abcf</outgroup_name>
                <bill_type xsi:type="xsd:string">prepaid12</bill_type>
            </s-gensym11>
        </get_cdrsResponse>
    </soap:Body>
</soap:Envelope>';

$sxe = new SimpleXMLElement($xml);
$response = $sxe->children('soap', true)->Body->children()->get_cdrsResponse;

foreach ( $response->children() as $gensym ) {
    echo $gensym->scustomer, PHP_EOL;
    echo $gensym->recording, PHP_EOL;
    echo $gensym->outgroup_name, PHP_EOL;
    echo $gensym->bill_type, PHP_EOL;
    echo PHP_EOL;
    echo PHP_EOL;
}

关于php - 无法从 Soap 响应中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15413518/

相关文章:

php - PDO 更新不更新数据

xml - 优化原始 SOAP 请求和响应的一些有用技术是什么?

php - SOAP 空信封。如何处理空响应

c# - 使用 Xpath 的 Xdocument

php - 在 WordPress 中向自定义帖子添加第二个标题?

php - 使用 Laravel 4 遍历多个请求文件

php - 确定字符串是否包含数组中的一组单词中的一个

java - 如何在处理 SOAP 消息时转换 SAAJ 对象?

java - fn : functions in XPathExpressions cause Exceptions in JDK1. 6

c# - 使用 HtmlAgilityPack 从 html 中提取值