我的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 :(得分:0)
为什么不首先测试属性?
if( is_object( $node ) && $node->hasAttribute( $attrib ) ).... etc