Php 使用wp_remote_在WordPress中使用XML
我怀疑我遗漏了一些非常基本和明显的东西,所以请提前道歉 我一直在使用Php 使用wp_remote_在WordPress中使用XML,php,xml,wordpress,Php,Xml,Wordpress,我怀疑我遗漏了一些非常基本和明显的东西,所以请提前道歉 我一直在使用simple\u xml\u load来处理xml文件,但我客户的托管提供商通过此方法阻止了外部文件的加载。我现在正试图使用WordPress内置的wp\u remote\u get功能重建我的工作 以下是我的代码(注意:在本例中,密钥和避难所ID是通用的): 使用它,我可以检索一个包含我需要的所有数据的数组,但我无法确定如何针对特定的数据。下面是打印($xml)的输出: 例如,如果我想回显状态代码,我该怎么做?使用simple
simple\u xml\u load
来处理xml文件,但我客户的托管提供商通过此方法阻止了外部文件的加载。我现在正试图使用WordPress内置的wp\u remote\u get
功能重建我的工作
以下是我的代码(注意:在本例中,密钥和避难所ID是通用的):
使用它,我可以检索一个包含我需要的所有数据的数组,但我无法确定如何针对特定的数据。下面是打印($xml)的输出:
例如,如果我想回显
状态代码,我该怎么做?使用simplexml,我将编写$xml->header->status->code
。我似乎不知道代码结构是什么,它可以使用wp\u remote\u get
对数组执行类似的操作
提前谢谢 到目前为止,您的代码将XML检索为字符串:
$url = "http://api.petfinder.com/shelter.getPets?key=1234&count=20&id=abcd&status=A&output=full";
$response = wp_remote_get($url);
$body = wp_remote_retrieve_body($response);
要将字符串(而不是URL)加载到SimpleXMLElement
(您没有显示具体的代码,因此我假设您是根据您的描述这样做的)中,您现在需要使用以下内容加载字符串:
我稍微更改了变量名(特别是使用
wp\u remote.*
函数),以便更清楚地了解这些变量的含义。做到了!非常感谢!
<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
<header>
<version>0.1</version>
<timestamp>2013-03-09T15:03:46Z</timestamp>
<status>
<code>100</code>
<message/>
</status>
</header>
<lastOffset>5</lastOffset>
<pets>
<pet>
<id>13019537</id>
<name>Jordy</name>
<animal>Dog</animal>
</pet>
<pet>
<id>13019888</id>
<name>Tom</name>
<animal>Dog</animal>
</pet>
</pets>
</petfinder>
$url = "http://api.petfinder.com/shelter.getPets?key=1234&count=20&id=abcd&status=A&output=full";
$response = wp_remote_get($url);
$body = wp_remote_retrieve_body($response);
$xml = simplexml_load_string($body);
$code = $xml->header->status->code;