YouTube XML Feed Error


YouTube XML Feed Error

我在一个网站上有一个页面,它从YouTube上提取我最喜欢的提要并将它们嵌入到网站中。

问题是它的工作80%的时间,但其他20%的时间我在页面上得到错误-代码中没有任何变化导致这种情况,所以我想知道什么可能导致这种情况,或者是否有更好的方法来做我正在做的事情…

当检索XML提要时,我得到的错误是403禁止…下面是它的样子(注意:行号不会完全匹配,因为我已经简化了下面的代码示例。

有问题的XML提要在这里: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";
    }
    ?>

您应该根据Google错误验证文档验证$sxml = simplexml_load_file($feedURL);的结果。然后,您可以打印出与403代码一起出现的实际消息,或者可能决定重试请求。如果它是随机发生的,我猜是配额限制问题,但实际的错误信息可能会告诉您您想知道的确切信息。

MYUSERNAME不是合法的用户名。添加您自己的youtube用户名!

当我在浏览器中调用feed URL (https://gdata.youtube.com/feeds/api/users/wfptv/favorites)时,我收到这个错误:

 Favorites of requested user are not public.

让你的提要公开,失败应该就消失了。