本文介绍了如何使用 php Guzzle 库发送 http 请求并解析 xml 响应数据。重点讲解了如何处理带有命名空间的 XML 数据,并提供示例代码演示如何提取 XML 中的特定字段,例如 ID 和 NAME,最终将数据转换为 key => value 数组形式。
在使用 PHP Guzzle 库进行 API 请求时,经常会遇到返回 XML 格式的数据。如果 XML 数据包含命名空间,解析起来可能会比较棘手。本教程将演示如何使用 SimpleXMLElement 类来解析带有命名空间的 XML 数据,并提取所需的信息。
使用 Guzzle 发送请求并获取 XML 响应
首先,我们需要使用 Guzzle 库发送 HTTP 请求,并获取 XML 响应。以下是示例代码:
立即学习“PHP免费学习笔记(深入)”;
<?php require 'vendor/autoload.php'; // 引入 Guzzle 库 use GuzzleHttpClient; $api_url = 'http://example.com/test.asmx/GetUserDetails?userID=123'; $client = new Client(); $response = $client->request('GET', $api_url); $xml_String = $response->getBody()->getContents(); echo $xml_string; // 输出 XML 字符串 ?>
确保已经通过 composer 安装了 Guzzle 库:composer require guzzlehttp/guzzle。
使用 SimpleXMLElement 解析 XML 数据
接下来,我们将使用 SimpleXMLElement 类来解析 XML 数据。如果 XML 数据包含命名空间,我们需要特别注意。
<?php $xml = new SimpleXMLElement($xml_string); // 处理命名空间 $diffgr = $xml->children('diffgr', true); $new_dataset = $diffgr->diffgram->NewDataSet; $table = $new_dataset->Table; // 提取数据 $id = (string) $table->ID; $name = (string) $table->NAME; // 构建 key => value 数组 $data = [ 'ID' => $id, 'NAME' => $name, ]; print_r($data); // 输出结果数组 ?>
代码解释:
- $xml = new SimpleXMLElement($xml_string);:将 XML 字符串转换为 SimpleXMLElement 对象。
- $diffgr = $xml->children(‘diffgr’, true);:获取名为 “diffgr” 的命名空间下的子元素。第二个参数 true 表示启用命名空间处理。
- $new_dataset = $diffgr->diffgram->NewDataSet; 和 $table = $new_dataset->Table;:逐层访问 XML 结构,获取 Table 元素。
- $id = (string) $table->ID; 和 $name = (string) $table->NAME;:提取 ID 和 NAME 元素的值,并将其转换为字符串类型。
- $data = [‘ID’ => $id, ‘NAME’ => $name];:将提取的 ID 和 NAME 值构建成 key => value 数组。
注意事项:
- 确保 XML 字符串有效,否则 SimpleXMLElement 可能会抛出异常。
- 如果 XML 结构复杂,需要根据实际情况调整代码中的元素访问路径。
- 如果 XML 中存在多个同名元素,可以使用循环遍历的方式提取数据。
完整示例代码:
<?php require 'vendor/autoload.php'; use GuzzleHttpClient; $api_url = 'http://example.com/test.asmx/GetUserDetails?userID=123'; // 替换为你的 API URL $client = new Client(); $response = $client->request('GET', $api_url); $xml_string = $response->getBody()->getContents(); // 示例 XML 数据 (替换为你的实际 XML 数据) $xml_string = '<?xml version="1.0" encoding="utf-8"?> <DataSet xmlns="http://example.com"> <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Table"> <xs:complexType> <xs:sequence> <xs:element name="ID" type="xs:string" minOccurs="0" /> <xs:element name="NAME" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> <NewDataSet xmlns=""> <Table diffgr:id="Table1" msdata:rowOrder="0"> <ID>123</ID> <NAME>EXAMPLE</NAME> </Table> </NewDataSet> </diffgr:diffgram> </DataSet>'; $xml = new SimpleXMLElement($xml_string); // 处理命名空间 $diffgr = $xml->children('diffgr', true); $new_dataset = $diffgr->diffgram->NewDataSet; $table = $new_dataset->Table; // 提取数据 $id = (string) $table->ID; $name = (string) $table->NAME; // 构建 key => value 数组 $data = [ 'ID' => $id, 'NAME' => $name, ]; print_r($data); ?>
总结
通过本教程,你学习了如何使用 PHP Guzzle 库发送 HTTP 请求,并使用 SimpleXMLElement 类解析带有命名空间的 XML 响应数据。掌握了处理命名空间的关键步骤,并学会了如何提取 XML 中的特定字段,最终将数据转换为 key => value 数组形式。希望本教程能够帮助你更好地处理 XML 数据,提高开发效率。
评论(已关闭)
评论已关闭