
本文旨在指导开发者如何使用 PHP 解析包含 pinBlocked 标签的 SOAP XML 响应。我们将使用 SimpleXMLElement 类来解析 XML,并通过注册命名空间和使用 XPath 查询来提取所需的标签值。本文提供了经过验证的代码示例,并针对不同的 PHP 版本提供了兼容方案,确保您可以轻松地将此方法应用于您的项目中。
在 PHP 中解析 SOAP XML 响应并提取特定标签的值,通常需要以下步骤:
假设我们有以下 SOAP XML 响应存储在 $result 变量中:
<?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>以下代码演示了如何使用 SimpleXMLElement 和 XPath 来提取 pinBlocked 标签的值:
立即学习“PHP免费学习笔记(深入)”;
<?php
$result = '<?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>';
$xml = new SimpleXMLElement($result);
// 注册命名空间
$xml->registerXPathNamespace('a', 'http://theapi.com');
// 使用 XPath 查询
$item = $xml->xpath('//a:pinBlocked');
// 提取标签值
echo $item[0]; // 输出 "true"
?>代码解释:
对于 PHP 5.3.3,上述代码可能需要进行一些修改,因为该版本对 SimpleXMLElement 的处理方式略有不同。以下是与 PHP 5.3.3 兼容的代码示例:
<?php
$result = '<?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>';
$xml = new SimpleXMLElement($result);
// 注册命名空间
$xml->registerXPathNamespace('a', 'http://theapi.com');
// 使用 XPath 查询
$item = $xml->xpath('//a:pinBlocked');
// 提取标签值
echo (string)$item[0]; // 输出 "true"
?>主要区别:
本文介绍了如何使用 PHP 解析 SOAP XML 响应并提取 pinBlocked 标签的值。通过使用 SimpleXMLElement 类、注册命名空间和使用 XPath 查询,您可以轻松地从 XML 文档中提取所需的数据。此外,本文还提供了与 PHP 5.3.3 兼容的代码示例,并强调了在实际应用中需要注意的一些事项。希望本教程能帮助您更好地处理 PHP 中的 XML 数据。
以上就是使用 PHP 解析 SOAP XML 响应并获取 pinBlocked 标签的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号