【问题标题】:PHP Fatal error on my youtube script我的 youtube 脚本上的 PHP 致命错误
【发布时间】:2015-03-31 11:05:08
【问题描述】:

我的 Youtube php 脚本出现以下错误:

Fatal error: Call to a member function getAttribute() on a non-object
in /home/web/public_html/youtube/home.php on line 8

代码是:

<?php
$vidID= $_GET['id'];
$url = "http://gdata.youtube.com/feeds/api/videos/". $vidID;
$doc = new DOMDocument;
$doc->load($url);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
$published = $doc->getElementsByTagName("published")->item(0)->nodeValue;
$duration = $doc->getElementsByTagName('duration')->item(0)->getAttribute('seconds');
//$durationformatted = ;str_pad(floor($duration/60), 2, '0', STR_PAD_LEFT) . ':' . str_pad($duration%60, 2, '0', STR_PAD_LEFT); not working atm :/
$name = $doc->getElementsByTagName("name")->item(0)->nodeValue;
$commentcount = $doc->getElementsByTagName('feedLink')->item(0)->getAttribute('countHint');
$averagerating = $doc->getElementsByTagName('rating')->item(0)->getAttribute('average');
$numberofraters = $doc->getElementsByTagName('rating')->item(0)->getAttribute('numRaters');
$statisticsviewcount = $doc->getElementsByTagName('statistics')->item(0)->getAttribute('viewCount');

echo "Title: ".$title."\nDuration: ".$duration." seconds\nPublished: ".$published."\nUsername/channel: ".$name."\nTotal Comments: ".$commentcount."\nCategory: ".$category."\nAverage rating: ".$averagerating." out of 5\nNumber of raters: ".$numberofraters."\nView count: ".$statisticsviewcount."\n";

?>

我该如何解决这个问题?

【问题讨论】:

  • 它可能实际上没有找到视频 - 这意味着没有项目0

标签: php youtube youtube-api youtube-data-api


【解决方案1】:

为什么不先测试属性?

if( is_object( $node ) && $node->hasAttribute( $attrib ) ).... etc

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多