【问题标题】:How to get the pinBlocked tag from the following SOAP XML response in PHP如何从 PHP 中的以下 SOAP XML 响应中获取 pinBlocked 标记
【发布时间】:2021-12-13 09:23:18
【问题描述】:

如何在 PHP 中从以下 SOAP XML 响应中获取 pinBlocked 标记

这是我的 SOAP XML 响应:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ReadCardStatusResponse xmlns="http://theapi.com"><result>1</result><errorMessage>ok</errorMessage><status><terminalID>123456789</terminalID><profileNumber>123456789</profileNumber><reference>37292141</reference><valid>true</valid><pinBlocked>true</pinBlocked><activated>true</activated><retired>false</retired><loaded>true</loaded><redeemed>true</redeemed><empty>true</empty><cancelled>false</cancelled><stopped>true</stopped><lost>false</lost><stolen>false</stolen><expired>false</expired><transactionID>blahblah</transactionID><transactionDate>2004-10-28T08:54:27</transactionDate><checksum>blahblah</checksum><resultCode>1</resultCode><resultText>ok</resultText></status></ReadCardStatusResponse></soap:Body></soap:Envelope>

响应保存在 $result 中

我试过了

$result->pinBlocked;

【问题讨论】:

    标签: php xml soap


    【解决方案1】:

    您需要使用您的 xml 作为字符串创建 SimpleXMLElement 的对象,然后使用随机前缀为 http://theapi.com 注册命名空间(在本例中我使用 a),然后使用 xpath 获取您的元素带有以前注册的前缀。

    $xml = new SimpleXMLElement($result);
    
    $xml->registerXPathNamespace('a', 'http://theapi.com');
    
    echo (bool)$xml->xpath('//a:pinBlocked')[0];
    

    对于 PHP 5.3.3 使用这个:

    $xml = new SimpleXMLElement($result);
    
    $xml->registerXPathNamespace('a', 'http://theapi.com');
    
    $item = $xml->xpath('//a:pinBlocked');
    
    echo $item[0];
    

    【讨论】:

    • Hmmmm 回显 (bool)$xml->xpath('//a:pinBlocked')[0];我在 php 中得到一个 synthax 错误
    • 你能给我完整的语法错误信息吗?
    • PHP 解析错误:语法错误,意外 '[',期待 ',' 或 ';'在第 102 行的 suhayl.php 中
    • 您可能正在使用低于 5.4 的 PHP 版本,因此您不能直接取消引用数组,在这种情况下这样做:$item = $xml-&gt;xpath('//a:pinBlocked'); echo $item[0];
    • im 使用 PHP 5.3.3 (cli)(构建时间:2017 年 3 月 22 日 12:27:09)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多