YouTube XML Feed Error - php

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

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.

MYUSERNAME is not a valid username. Add your own youtube username!

When I call your feed URL in browser (https://gdata.youtube.com/feeds/api/users/wfptv/favorites) I receive this error:
Favorites of requested user are not public.
Make your feed public, and the failure should be gone.

Related

PHP wont load XML file [duplicate]

This question already has answers here:
DOMDocument - Load xml rss - failed to open stream
(2 answers)
Closed 7 years ago.
I am writing a web app and I am trying access a RSS feed from the financial times. However when I run my code I get the following error. I was hoping someone could tell me where I've went wrong.
function getFtRSS(){
//Create a object to parse XML input from Finanial Time's UK companies RSS feed
$rssFeed = new DOMdocument();
//Gather the information and load it into the object
$rssFeed->load('http://www.ft.com/rss/companies/uk');
$articles = array();
//Lookp through all elements in the XML document
foreach($rssFeed->getElementsByTagName('item') as $newsArtcle){
$title = $newsArtcle->getElementsByTagName('title')->item(0)->nodeValue;
$desc = $node->getElementsByTagName('description')->item(0)->nodeValue;
$link = $node->getElementsByTagName('link')->item(0)->nodeValue;
$date = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
echo '<li class="list-group-item news-item" onclick="window.location=\'$link\'">
<h3 class=\'top-element\'>$title</h3>
<h4>$desc</h4>
<h5>$date</h5>
</li>';
}
}
I get the following error from my program:
Warning: DOMDocument::load(http://www.ft.com/rss/companies/uk): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /home/ubuntu/workspace/MOT/includes/functions.php on line 46
Have you checked the request out of the PHP ? I noticed that they don't accept requests with bad UA check the screenshot below;
Update: Yes they do not accept bad UA, you should change your data fetching method.

403 error when using file_get_contents()

I'm trying to scrape some product details from a website using the following code:
$list_url = "http://www.topshop.com/en/tsuk/category/sale-offers-436/sale-799";
$html = file_get_contents($list_url);
echo $html;
However, I'm getting this error:
Warning:
file_get_contents(http://www.topshop.com/en/tsuk/category/sale-offers-436/sale-799)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.0 403 Forbidden in
/homepages/19/d361310357/htdocs/shopaholic/rss/topshop_f_uk.php on
line 123
I gather that this is some sort of block by the website to prevent scraping. Is there a way around this - perhaps using cURL and setting a user agent?
If not, is there another way of getting basic product data like item name and price?
EDIT
The context of my code is that I'd eventually still want to be able to achieve the following:
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
I've managed to fix it by adding the following code...
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)');
...as per this answer.
You should use cURL , not the simple way with file_get_contents().
Use cURL and set up the proper http headers to mimic a proper http request (a real request).
P.S. : set up cURL to follow redirects . Here is the link to cURL

Rss feed with PHP

Hi can Anyone tell me how to process this rss feed in php
http://www.ft.com/rss/companies/travel-leisure
when i am executing the below lines
$rss = new DOMDocument();
$rss->load('http://www.ft.com/rss/companies/travel-leisure');
it is giving an error
A PHP Error was encountered
Severity: Warning
Message: DOMDocument::load(): Opening and ending tag mismatch: link line 8 and head in http://www.ft.com/rss/companies/travel-leisure, line: 11
Thanks
If you request http://www.ft.com/rss/companies/travel-leisure without a User-Agent HTTP request header, you can get an error message back (under a 200 OK status).
This is a bug in FT's website.
As a work around, I suggest using cURL to fetch the data, and then feed a string into DOMDocument.
Why not use a dedicated library like SimplePie to process your RSS feed ?
$feed = new SimplePie('http://www.ft.com/rss/companies/travel-leisure');
$feed->init();
$feed->handle_content_type();
foreach ($feed->get_items() as $item) {
$permalink = $item->get_permalink();
$title = $item->get_title();
// Do what you want...
}
I have not tested this code, it's just to show you an example of use. Here the documentation and an example for more explanations.

simplexml_load_file 403 Forbidden

Warning: simplexml_load_file(http://gdata.youtube.com/feeds/api/videos/b3a5BgqObEY): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
is the error that I get with the youtube feed in my site. I was told it was because a YouTube video was removed, but I don't know enough about PHP to fix this error. Can someone help push me in the right direction? When I searched for this error, all that came up was xml stuff and this is all in php so that doesn't help.
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/'. $youtube->yt_videoId;
// read feed into SimpleXML object
$entry = simplexml_load_file($feedURL);
// parse video entry
$video = $main->parseVideoEntry($entry);
I'll keep looking, but thanks in advance if you have a good link for me to look at while I research this problem.
If you visit the url http://gdata.youtube.com/feeds/api/videos/b3a5BgqObEY you'll see "Private video", which is the reason you can't get the feed...
EDIT: You can use the V3 of the API to send an authentificated request; check this url to learn how to do it: https://developers.google.com/youtube/v3/
Greetings

How can i validate an XML feed before parsing it with simplexml_load_file in PHP?

I am doing this at the moment
<?php
$xml = simplexml_load_file('www.tompeters.com/atom.xml');
print_r($xml);
?>
but it returns this error
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "www.tompeters.com/atom.xml"
I think it is because the feed isn't valid XML?
So how can I validate it so I can parse the feed?
Thanks
Parsing the feed is validating it.
Anyway, the error message looks more like that you don't have access to that file. I don't know PHP, but I would have expected at least
simplexml_load_file('http://www.tompeters.com/atom.xml');

Categories