2011-08-17 72 views
3

我在一個網站上有一個頁面,這個頁面從YouTube上拉我的收藏夾並將它們嵌入到網站中。YouTube XML Feed Error

問題是,它的工作時間是80%,但其他20%的時間我在頁面上發生錯誤 - 代碼中沒有任何內容正在改變,導致這一點,所以我想知道什麼可能是造成這種情況,或者如果有更好的方式來做我在做什麼...

我得到的錯誤是一個403禁止當檢索XML飼料...這是它的樣子(注意:行號將不完全匹配,因爲我簡化了以下代碼示例。

有問題的XML Feed在這裏: https://gdata.youtube.com/feeds/api/users/umarchives/favorites

Warning: simplexml_load_file(https://gdata.youtube.com/feeds/api/users/umarchives/favorites) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42 

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "https://gdata.youtube.com/feeds/api/users/umarchives/favorites" in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42 

Warning: Invalid argument supplied for foreach() in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 47 

下面是我使用的代碼:

<?php 
    // set feed URL 
    $YouTubeUsername = "umarchives"; 

    $feedURL = "https://gdata.youtube.com/feeds/api/users/".$YouTubeUsername."/favorites"; 

    // read feed into SimpleXML object 
    $sxml = simplexml_load_file($feedURL); 

    // iterate over entries in feed 
    foreach ($sxml->entry as $entry) { 

    // get nodes in media: namespace for media information 
    $media = $entry->children('http://search.yahoo.com/mrss/'); 
    $attrs = $media->group->content->attributes(); 

    $videoURL = $attrs['url']; 
    $videoURL = preg_replace('/\?.*/', '', $videoURL); 
    $videoURL = str_replace("/v/","/embed/",$videoURL); 

    $videoTitle = $media->group->title; 

    echo "<iframe class='youtube-player' width='300' height='225' src='$videoURL'></iframe>\n"; 
    echo "<br>\n"; 

    } 

    ?> 
+0

什麼錯誤? – powtac

+0

對不起,我將它添加到問題中,檢索XML Feed時出現403 Forbidden錯誤... – Dan

+0

錯誤看起來像什麼? – powtac

回答

3

您應該驗證$sxml = simplexml_load_file($feedURL);per the Google error validation docs的結果。然後,您可以打印出403代碼附帶的實際消息,或者可能決定重試請求。如果這是一個隨機事件,我的猜測是一個配額限制問題,但實際的錯誤信息可能會告訴你到底你想知道什麼。

+0

這很棒,感謝鏈接和洞察力mjhm! – Dan

0

MYUSERNAME不是一個有效的用戶名。添加您自己的YouTube用戶名!

+0

謝謝,但爲了這個例子我只是取而代之。它確實具有正確的用戶名,並且再次,80%的時間工作... – Dan

+0

嘿丹,你的錯誤表明你正在使用MYUSERNAME。請更新你的錯誤! – powtac

+0

錯誤消息現在使用propery用戶名更新。只要收藏夾提要可公開查看,您就可以用任何YouTube用戶名替換它。您也可以直接在這裏查看XML feed:https://gdata.youtube.com/feeds/api/users/umarchives/favorites' – Dan

0

當我打電話在瀏覽器中的飼料URL(https://gdata.youtube.com/feeds/api/users/wfptv/favorites)我收到此錯誤:

Favorites of requested user are not public. 

讓你的飼料公,失敗應該消失。