无法解决节点不再存在错误
我正在尝试使用 youtube xml 显示一些数据,但弹出此错误。 从理论上讲,我什至知道出了什么问题,
$xmlData = simplexml_load_string(utf8_encode(file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$v.'?fields=title,yt:recorded,yt:statistics'))); //$v is video array
$title = (string)$xmlData->title;
$entry = $xmlData;
$namespaces = $entry->getNameSpaces(true);
$yr = $entry->children((string)$namespaces['yt']);
// get <yt:recorded> node for date and replace yyyy-mm-dd to dd.mm.yyyy
$year = substr($yr->recorded, 0,4);
$month = substr($yr->recorded, 5,2);
$day = substr($yr->recorded, 8,2);
$recorddate = $day.".".$month.".".$year;
// get <yt:stats> node for viewer statistics, and here the problem starts (error appears if view count is 0 / node does not exist)
$attrs = $yr->statistics->attributes();
$viewCount = $attrs[(string)'viewCount'];
{ echo '<p>'.$recorddate.'<br>'.$title.'<br>';
if ($viewCount > 0)
echo $viewCount.'</p></div>';
else
echo '(show some other text)</p></div>'; }
我知道要解决这个问题,您必须告诉 php 节点是字符串,但我仍然无法在不破坏其余代码的情况下做到这一点
I am trying to use youtube xml to show some data but this error pops up.
In teory I even know what`s wrong
$xmlData = simplexml_load_string(utf8_encode(file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$v.'?fields=title,yt:recorded,yt:statistics'))); //$v is video array
$title = (string)$xmlData->title;
$entry = $xmlData;
$namespaces = $entry->getNameSpaces(true);
$yr = $entry->children((string)$namespaces['yt']);
// get <yt:recorded> node for date and replace yyyy-mm-dd to dd.mm.yyyy
$year = substr($yr->recorded, 0,4);
$month = substr($yr->recorded, 5,2);
$day = substr($yr->recorded, 8,2);
$recorddate = $day.".".$month.".".$year;
// get <yt:stats> node for viewer statistics, and here the problem starts (error appears if view count is 0 / node does not exist)
$attrs = $yr->statistics->attributes();
$viewCount = $attrs[(string)'viewCount'];
{ echo '<p>'.$recorddate.'<br>'.$title.'<br>';
if ($viewCount > 0)
echo $viewCount.'</p></div>';
else
echo '(show some other text)</p></div>'; }
I know that to resolve this you must tell php that node is string but I still have not managed to do it without breaking the rest of code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很好,但是对于那些使用 5.3.0 之前版本的 php 的人来说,这不起作用:(
有更好的解决方案吗?
nice, but for those with php prior 5.3.0 this will not work :(
any better solutions?
解决方案也适用于 php 用户 <= 5.2
Solution that should work also for php users <= 5.2
我刚刚遇到这个问题。似乎您需要先检查是否有可用的统计信息,然后尝试访问属性。
如果视频尚未被观看,YouTube 似乎不会添加这些属性。因此,如果您的视频观看次数为 0,YouTube 不会将 viewCount 设为 0,而是完全忽略它。
I just had this issue. Seems like you need to check if any statistics are available first, then try to access the attributes.
YouTube doesn't seem to add the attributes if the video hasn't been viewed. So if you've got a video with a view count of 0, YouTube doesn't put the viewCount as 0, it just omits it altogether.
您没有正确使用转换。此行不将
SimpleXMLElement
对象转换为字符串。 (它将字符串'viewCount'
转换为字符串,这是没有意义的)这个的作用是:
You're not using casting correctly. This line does NOT cast the
SimpleXMLElement
object to a string. (it cast the string'viewCount'
to a string, which is non-sensical)This one does: