我在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." secondsnPublished: ".$published."nUsername/channel: ".$name."nTotal Comments: ".$commentcount."nCategory: ".$category."nAverage rating: ".$averagerating." out of 5nNumber of raters: ".$numberofraters."nView count: ".$statisticsviewcount."n";
?>
我该怎么解决这个问题?
为什么不先测试属性?
if( is_object( $node ) && $node->hasAttribute( $attrib ) ).... etc