无法解决节点不再存在错误

发布于 2024-11-05 00:56:49 字数 1194 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随梦而飞# 2024-11-12 00:56:49
$viewCount = 0;
if ($yr->statistics->count() > 0) {
$attrs = $yr->statistics->attributes();
 $viewCount = $attrs['viewCount'];
}

很好,但是对于那些使用 5.3.0 之前版本的 php 的人来说,这不起作用:(
有更好的解决方案吗?

$viewCount = 0;
if ($yr->statistics->count() > 0) {
$attrs = $yr->statistics->attributes();
 $viewCount = $attrs['viewCount'];
}

nice, but for those with php prior 5.3.0 this will not work :(
any better solutions?

财迷小姐 2024-11-12 00:56:49

解决方案也适用于 php 用户 <= 5.2

  $viewCount = 0;
if (count($yr->statistics) > 0) {
    $attrs = $yr->statistics->attributes(); 
    $viewCount = $attrs['viewCount'];
}

Solution that should work also for php users <= 5.2

  $viewCount = 0;
if (count($yr->statistics) > 0) {
    $attrs = $yr->statistics->attributes(); 
    $viewCount = $attrs['viewCount'];
}
埖埖迣鎅 2024-11-12 00:56:49

我刚刚遇到这个问题。似乎您需要先检查是否有可用的统计信息,然后尝试访问属性。

$viewCount = 0;
if ($yr->statistics->count() > 0) {
    $attrs = $yr->statistics->attributes(); 
    $viewCount = $attrs['viewCount'];
}

如果视频尚未被观看,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.

$viewCount = 0;
if ($yr->statistics->count() > 0) {
    $attrs = $yr->statistics->attributes(); 
    $viewCount = $attrs['viewCount'];
}

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.

青芜 2024-11-12 00:56:49

您没有正确使用转换。此行SimpleXMLElement 对象转换为字符串。 (它将字符串 'viewCount' 转换为字符串,这是没有意义的)

$viewCount = $attrs[(string)'viewCount'];

这个的作用是:

$viewCount = (string) $attrs['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)

$viewCount = $attrs[(string)'viewCount'];

This one does:

$viewCount = (string) $attrs['viewCount'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文