2013-05-20 382 views
2

我想從使用api的youtube得到discriminationptiobn ....但我不知道我在哪裏錯了它沒有得到單個視頻。但如果我嘗試播放列表其做工精細,不帶視頻IDYoutube Api獲取視頻描述與視頻ID

這裏是我的代碼

error_reporting(E_ALL); 

$feedURL = 'https://gdata.youtube.com/feeds/api/playlists/'.$id.'?v=2&prettyprint=true'; 
$sxml = simplexml_load_file($feedURL); 

echo $feedURL.'</br>'; 
foreach ($sxml->entry as $entry) 
{  
    echo $media->group->description; 
} 

上面的代碼正在與播放列表......但如果我嘗試一個視頻它不工作:

error_reporting(E_ALL); 

$feedURL = 'http://gdata.youtube.com/feeds/api/videos/'.$id.'?v=2&alt=json&prettyprint=true'; 
$sxml = simplexml_load_file($feedURL); 

echo $feedURL.'</br>'; 
foreach ($sxml->entry as $entry) 
{   
    echo $media->group->description; 
} 

回答

5

如果您通過'alt = json'參數調用YouTube api,則該響應將被格式化爲JSON而不是XML。所以,你應該使用:

$response = json_decode(file_get_contents('... API URL ...'), true); 

然後 '說明' 可以訪問:

$response['entry']['media$group']['media$description']['$t'] 
+0

THX Barell ....它幫我解決我的問題....;)...有在網址中出現問題...它應該是'http://gdata.youtube.com/feeds/api/videos/fU-zI53ddGg'.$id',並保持所有相同的工作正常,thx;) – Harinder