使用PHP调用URL并获取XML响应
因此,我尝试在调用带有参数的URL(get请求)后获取XML响应。我在下面找到了这个代码,它正在工作使用PHP调用URL并获取XML响应,php,xml,get,response,Php,Xml,Get,Response,因此,我尝试在调用带有参数的URL(get请求)后获取XML响应。我在下面找到了这个代码,它正在工作 $url = "http://..."; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT
$url = "http://...";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$response = curl_exec($ch);
curl_close($ch);
echo $response;
但是作为回应,我得到了一个没有逗号的大字符串(所以我不能分解它)。这个字符串只有值,没有键
有没有办法获得关联数组
XML类似于:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<transaction>
<date>2011-02-10T16:13:41.000-03:00</date>
<code>9E884542-81B3-4419-9A75-BCC6FB495EF1</code>
<reference>REF1234</reference>
<type>1</type>
<status>3</status>
<paymentMethod>
<type>1</type>
<code>101</code>
</paymentMethod>
<grossAmount>49900.00</grossAmount>
<discountAmount>0.00<discountAmount>
(...)
因此,我希望有一个如下的数组:
date => ...
code => ...
reference => ...
(and so on)
可能吗?如果是,怎么做
编辑:我不同意“这个问题已经回答了”的标签。在指定主题上找不到解决我的问题的代码。但是,无论如何,我找到了一种方法,使用下面的代码
$url = http://...;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$transaction= curl_exec($curl);
curl_close($curl);
$transaction = simplexml_load_string($transaction);
var_dump($transaction); //retrieve a object(SimpleXMLElement)
我使用这样的方法(非常通用的解决方案):
唯一的问题是我排除了属性部分,因为我的案例不需要它们
<?php
class xml2array {
function xml2array($xml) {
if (is_string($xml)) {
$this->dom = new DOMDocument;
$this->dom->loadXml($xml);
}
return FALSE;
}
function _process($node) {
$occurance = array();
foreach ($node->childNodes as $child) {
$occurance[$child->nodeName]++;
}
if ($node->nodeType == XML_TEXT_NODE) {
$result = html_entity_decode(htmlentities($node->nodeValue, ENT_COMPAT, 'UTF-8'),
ENT_COMPAT,'ISO-8859-15');
} else {
if($node->hasChildNodes()){
$children = $node->childNodes;
for ($i=0; $i < $children->length; $i++) {
$child = $children->item($i);
if ($child->nodeName != '#text') {
if($occurance[$child->nodeName] > 1) {
$result[$child->nodeName][] = $this->_process($child);
} else {
$result[$child->nodeName] = $this->_process($child);
}
} else if ($child->nodeName == '#text') {
$text = $this->_process($child);
if (trim($text) != '') {
$result[$child->nodeName] = $this->_process($child);
}
}
}
}
}
return $result;
}
function getResult() {
return $this->_process($this->dom);
}
}
?>
该代码是非常自解释的、客观的方法,可以很容易地修改以排除或包含所需的部分
只需将XML加载到DOM对象中,然后递归地检查子对象并获取相应的值
希望它能有所帮助我很幸运地使用了这样的代码:
$obj = new xml2array($response);
$array = $obj->getResult();
$url = "http://feeds.bbci.co.uk/news/rss.xml";
$xml = file_get_contents($url);
if ($rss = new SimpleXmlElement($xml)) {
echo $rss->channel->title;
}
您可以使用这两种方式,最简单的是将所有内容放入数组中并通过json编码发送。另一个是必须通过添加标题和内容来生成xml。Orel,谢谢,但我忘了提到我收到的字符串只有值,没有键。Sundar,谢谢,但这如何适合我的代码?而我只收到了。我无法更改发件人所做的任何更改。您可以解析xml,它非常简单。看,这不是好运气,但是使用
SimpleXML
parseryour's right@michi,你在评论中发布的链接非常好。我试图添加一个片段,使海报能够快速工作。但是,它不起作用。我想原因是因为我的URL不像你的代码那样以“.xml”结尾(它有参数)。伙计,我不知道如何用你的代码来说明它是否有效。正如我所说,我有这个字符串,包含XML中的所有值(不含键)。因此,我的目标是修改我正在使用的代码,使其具有关联数组。