I have this xml coming from a API.
我有这个xml来自API。
<response>
<ads numAds="2">
<ad ranking="1">
<title>Artist News on Facebook®</title>
<description>
What's the most current music news? Sign up For Free to Find out!
</description>
<pixel_url>
http://cc.xdirectx.com/pixellink.php?qry=e8b6b726c
</pixel_url>
<url visibleurl="Facebook.com">
http://cc.xdirectx.com/clicklink.php?qry=067a9027
</url>
</ad>
<ad ranking="2">
<title>Artist News on Facebook®</title>
<description>
What's the most current music news? Sign up For Free to Find out!
</description>
<pixel_url>
http://cc.xdirectx.com/pixellink.php?qry=e8b6b726c
</pixel_url>
<url visibleurl="Facebook.com">
http://cc.xdirectx.com/clicklink.php?qry=067a9027
</url>
</ad>
</ads></response>
When I try to parse using simplexml_load_string or SimpleXMLElement I am getting this result.
当我尝试使用simplexml_load_string或SimpleXMLElement进行解析时,我得到了这个结果。
[ads] => SimpleXMLElement Object ( [@attributes] => Array ( [numAds] => 6 )
[ads] => SimpleXMLElement对象([@attributes] =>数组([numAds] => 6)
[ad] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ranking] => 1
)
[title] => Artist News on Facebook®
[description] => What's the most current music news? Sign up For Free to Find out!
[pixel_url] => http://cc.xdirectx.com/pixellink.php?qry=e8b6b726c
[url] => http://cc.xdirectx.com/clicklink.php?qry=067a9027
)
)
The important thing missing is url visibleurl attribute which I need. I tried looking online and wasted complete day in fixing this, but no answer. Can someone rectify the mistake I am doing?
缺少的重要事项是我需要的url visibleurl属性。我尝试在线查看并浪费了整整一天来解决这个问题,但没有答案。有人可以纠正我正在做的错误吗?
PHP CODE:
PHP代码:
$result = getCurlJson($urlParse);
$output = simplexml_load_string($result) or die("Error: Cannot create object");
echo '<pre>';
print_r($output);
echo '</pre>';
function getCurlJson($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
1 个解决方案
#1
1
The loading of the XML works fine, but print_r
is not able to print everything from an XML object
. See this repository for a solution if you want to dump the XML code: simplexml_debug
加载XML工作正常,但print_r无法从XML对象打印所有内容。如果要转储XML代码,请参阅此存储库以获取解决方案:simplexml_debug
You can access the attribute you want with
您可以访问所需的属性
echo $output->ads->ad[0]->url->attributes();
Which works perfectly fine.
哪个效果很好。
#1
1
The loading of the XML works fine, but print_r
is not able to print everything from an XML object
. See this repository for a solution if you want to dump the XML code: simplexml_debug
加载XML工作正常,但print_r无法从XML对象打印所有内容。如果要转储XML代码,请参阅此存储库以获取解决方案:simplexml_debug
You can access the attribute you want with
您可以访问所需的属性
echo $output->ads->ad[0]->url->attributes();
Which works perfectly fine.
哪个效果很好。