php – 无法从Soap响应中提取数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 无法从Soap响应中提取数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4540字,纯文字阅读大概需要7分钟。
内容图文
![php – 无法从Soap响应中提取数据](/upload/InfoBanner/zyjiaocheng/722/3d29aa1036b04475b1710449906ca741.jpg)
这是我正在使用的代码:
$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;
4458
abc
prepaid
======================================
UPDATE
======================================
处理多个子节点时,您可以忽略命名空间并使用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响应中提取数据全部内容,希望文章能够帮你解决php – 无法从Soap响应中提取数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。