【问题标题】:YouTube XML Feed ErrorYouTube XML 供稿错误
【发布时间】:2011-10-28 23:11:57
【问题描述】:

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

问题是它在 80% 的时间里都在工作,但在另外 20% 的时间里我在页面上遇到错误 - 代码中没有任何变化会导致这种情况,所以我想知道可能是什么原因造成的这个,或者如果有更好的方法来做我正在做的事情......

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

有问题的 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";

    }

    ?>

【问题讨论】:

  • 抱歉,我已将其添加到问题中,检索 XML 提要时出现 403 Forbidden 错误...
  • 错误到底是什么样的?
  • 好的,我已经添加了上面错误的确切文本,抱歉缺少细节...

标签: php xml api rss youtube


【解决方案1】:

您应该验证$sxml = simplexml_load_file($feedURL); per the Google error validation docs 的结果。然后您可以打印出 403 代码附带的实际消息,或者可能决定重试该请求。如果这是随机发生的,我的猜测是配额限制问题,但实际的错误信息可能会准确地告诉您您想知道的内容。

【讨论】:

    【解决方案2】:

    MYUSERNAME 不是有效的用户名。添加您自己的 youtube 用户名!

    【讨论】:

    • 谢谢,但为了这个例子,我只是替换了它。它确实有正确的用户名,而且,它确实有 80% 的时间都在工作......
    • 嘿,丹,您的错误表明您正在使用 MYUSERNAME。请更新您的错误!
    • 错误消息现在更新为属性用户名。只要收藏夹供稿是公开可见的,您就可以将其替换为任何 YouTube 用户名。您也可以在此处直接查看 XML 提要:https://gdata.youtube.com/feeds/api/users/umarchives/favorites
    【解决方案3】:

    当我在浏览器中调用您的供稿 URL (https://gdata.youtube.com/feeds/api/users/wfptv/favorites) 时,我收到此错误:

     Favorites of requested user are not public.
    

    公开您的提要,失败应该消失。

    【讨论】:

      猜你喜欢
      • 2010-12-03
      • 2012-09-21
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2016-10-20
      • 2016-04-25
      • 2013-04-05
      • 2022-10-16
      相关资源
      最近更新 更多