使用SimpleXML获取XML属性 [英] Get XML Attribute with SimpleXML

查看:168
本文介绍了使用SimpleXML获取XML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取$xml->entry->yt:statistics->attributes()->viewCount属性,并且我已经使用SimpleXML进行了一些尝试,但是我真的无法使其正常工作!

I'm trying to get the $xml->entry->yt:statistics->attributes()->viewCount attribute, and I've tried some stuff with SimpleXML, and I can't really get it working!

尝试#1

<?php
$xml = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos?author=Google");
echo $xml->entry[0]->yt:statistics['viewCount'];
?>

尝试#2

<?php
$xml = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos?author=Google");
echo $xml->entry[0]->yt:statistics->attributes()->viewCount;
?>

尽管SimpleXML可以正常工作,但两者都返回空白,我试图获取供稿的标题,有效了!

Both of which return blank, though SimpleXML is working, I tried to get the feed's title, which worked!

有什么想法吗?

我已经查看了SO和其他站点上其他示例的负载,但是以某种方式行不通吗? PHP会识别':'是断言,还是我只是在做一些愚蠢的事情?

I've looked at loads of other examples on SO and other sites, but somehow this isn't working? does PHP recognize the ':' to be a cut-off, or am I just doing something stupid?

谢谢您,任何回复都非常感谢!

Thank you, any responses greatly appreciated!

推荐答案

如果您只想获取youtube视频的观看次数,则必须指定视频ID.在每个视频网址中都可以找到youtube ID.例如"http://www.youtube.com/watch?v=ccI-MugndOU",因此ID为ccI-MugndOU.为了获得观看次数,请尝试下面的代码

If you just want to get the viewcount of a youtube video then you have to specify the video ID. The youtube ID is found in each video url. For example "http://www.youtube.com/watch?v=ccI-MugndOU" so the id is ccI-MugndOU. In order to get the viewcount then try the code below

$sample_video_ID = "ccI-MugndOU";
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos?q={$sample_video_ID}&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'};
    echo $views;

这篇关于使用SimpleXML获取XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆