php - 在 PHP(和/或 Zend Framework)中解析 XML 响应

标签 php xml zend-framework parsing

我正在使用 Zend Framework 并使用 Zend_Http_Client 向第三方 API 发出 POST 请求。

$client = new Zend_Http_Client('http://api.com');
$client->setParameterPost(array(
    'param1' => 'value'
));
$response = $client->request('POST');
echo $response->getBody();

此 API 返回一个 XML 文档作为其响应。

<?xml version="1.0" ?>
<registration>
    <id>12345</id>
</registration>

如何将响应转化为我可以使用的东西?

最佳答案

我发现最简单的方法是使用 SimpleXml

$data = simplexml_load_string($response->getBody());

然后,要获取ID,您可以使用

$id = (string) $data->registration->id;

关于php - 在 PHP(和/或 Zend Framework)中解析 XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4018931/

相关文章:

xml - XSLT xsl :normalize-space() function not working

python - 如何用 beautiful soup/python 重建 xml key?

php - 在没有实际框架的情况下使用 Zend Framework 组件?

javascript - Ionic + Angular - 如何在 POST 请求后避免 "404 Not Found (from cache)"?

php - Laravel 多态嵌套关系显示无法解释的行为

php - 如何在 Yii for Postgres 中将 float 绑定(bind)到 SQL 命令?

php - 当列不存在时,查询中的 "column"IS NOT NULL 不会引发错误

java - Spring Security "No bean named ' filterChainProxy'可用“错误

zend-framework - Zend 框架重定向器无法正常工作

zend-framework - 使用 SSL 和 MAMP 的 mod_rewrite 出现 404 错误