Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube XML Feed Error

Tags:

php

youtube

xml

rss

I've got a page on a website that's pulling my favorites feed from YouTube and embedding them into site.

The problem is it's working 80% of the time, but the other 20% of the time i'm getting errors on the page - nothing in the code is changing to cause this, so i'm wondering what might be causing this, or if there's a better way to do what i'm doing...

The Error I'm gettings is a 403 Forbidden when retrieving the XML feed... here's what it looks like (note: the line numbers won't match exactly, because i've simplified the code sample below.

The XML feed in question is here: 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

Here's the code i'm using:

<?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";

    }

    ?>
like image 484
Dan Avatar asked Sep 14 '26 11:09

Dan


1 Answers

You should be validating the result of $sxml = simplexml_load_file($feedURL); per the Google error validation docs. Then you can print out the actual message that comes along with the 403 code, or possibly decide to retry the request. If it's a random occurrence my guess is a quota limit issue, but the actual error information will likely tell you exactly what you want to know.

like image 180
mjhm Avatar answered Sep 16 '26 23:09

mjhm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!