2011-08-17 66 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. 

让你的饲料公,失败应该消失。