2015-03-31 36 views
0

致命錯誤我得到我的YouTube的PHP腳本以下錯誤:PHP在我的YouTube腳本

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」。 – 2015-03-31 11:08:56

+0

Youtube「thingy」? – 2015-03-31 11:09:00

回答

0

爲什麼不先測試屬性?

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