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.
Related
I'm trying to develop and Instagram application but I'm struggling to get a json inserting the access token via variable.
That's my code:
$personal = json_decode(file_get_contents('https://api.instagram.com/v1/users/self/?access_token={$accesstoken}'));
And the error that I receive is this:
PHP Warning: file_get_contents(https://api.instagram.com/v1/users/self/?access_token={$accesstoken}): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
I already tried to see if I get the variable $accesstoken from the previous form and it's all right, because if I echo-it on the page it pops up.
I tried the function preg_replace to understand if the problem were the white spaces, but nothing.
I don't want to use cURL if it is not mandatory.
What's wrong with the code (and me)?
Thanks in advance!
Edit:
As I answered to #FirstOne: yes, I already tried to put the variable (access token) manually and in this way it works.
This question already has answers here:
how to read xml file from url using php
(5 answers)
Closed 8 years ago.
The below mentioned URL Link sends a XML response in browser. The same response is received in a variable using PHP
In single PHP page I should raise the URL request and should get the response in the same page.
http://localhost:8090/solr/salesreport/select?q=salesdates:[2007-05-01 TO 2014-05-15]&wt=xml&rows=10&indent=true
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML(file_get_contents("http://203.196.191.92:8090/solr/salesreport/select?q=salesdates:[2007-05-01TO2014-05-13]&wt=xml&rows=10&indent=true"));
I found the solution:
$url = 'http://localhost:8090/solr/salesreport/select?q='.$grystrres.'&wt=xml&rows=10&indent=true';
$xml = simplexml_load_file($url);
print_r($xml);
$variable = file_get_contents('http://203.196.191.92:8090/solr/salesreport/select?q=salesdates:[2007-05-01TO2014-05-13]&wt=xml&rows=10&indent=true');
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
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.
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.