I've got a site "mysite.com", this has a rss/xml-feed located at mysite.com/feed.
I have to read this feed in a php-script but simplexml_load_file returns an empty result. How can I get the "real" path to the feed? I assume this file is created using some clever .htaccess and such.
I think it has nothing to do with .htaccess. to get the feed you have two options.
one is using file_get_content, the other is using cURL.
$xml = file_get_contents('http://mysite.com/feed');
and
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://mysite.com/feed');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
now the $xml hold the xml file, so you can use simplexml_load_file to parse it.
An alternative is you can use Rss PHP. more details are here
Related
I am browsing Github info/docs but cant find any simple example on how to
get contents of a raw Github file.
For example if I try to use
$url ="https://raw.githubusercontent.com/octocat/Spoon-Knife/master/index.html";
echo file_get_contents($url);
I get failed to open stream: HTTP...
If I use curl same thing 404 page. So obviously I have to use API but there is just no simple example on how to do it.
Can someone please post plain example.
Thank you!
You can use a different method to escape SSL validation and add more options if you want, with CURL function.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://raw.githubusercontent.com/octocat/Spoon-Knife/master/index.html');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
Works fine for me here, you just need to enable, if was not, the curl extension.
I'm try to read an external XML file with a PHP script, no wmatter what I try I get an "empty document" error. If I open the url in my browser I can access and read the xml fine.
There are numerous other posts on stack overflow with similar problem as mine but none of the solutions work in my case.
This is my code:
$url="http://xml.example.com";
$xml = new SimpleXMLElement("compress.zlib://$url", NULL, TRUE);
parser error : Document is empty in /home/admin/public_html/xml/index2.php on line 4
Apparently the 3rd party service requires that I explicitly request gzip compression.
Any ideas?
Thanks,
Alan.
Ok I got it working using curl then createing an XML object (I think!)
$ch = curl_init();
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
curl_close($ch);
//print_r($output);
$oXML = new SimpleXMLElement($output);
The results are compatible with my script and can be parsed to extract the data :0)
How do I receive an xml response using php curl? I don't need to manipulate the xml or store it or anything like that. I just need to receive it as plain xml. Does anyone know how to do this?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/path/to/service');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec($ch);
$store contains response.
Hi I would like to know how to download a file to a variable (or memory) with php. I have tried using file_get_contents and a few RSS feed php libraries with no output (even with var_dump). This is the url that I am trying to access:
http://www.facebook.com/feeds/friends_status.php?id=MY_USER_ID&key=SECRET_KEY&format=rss20
MY_USER_ID is my facebook id, SECRET_KEY is the secret key. I am using the RSS feed method because I have tried for approximately 5 hours to make a facebook canvas application that fetches my friend's names and statuses with no success.
EDIT: You can access the feed without 404 errors by replacing the "http" with "feed"
A lot of API's require a user agent string. So you could try setting that. You can use cURL to get the file, and change the headers. Try this function:
function getUrl($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
Usage:
$file = getUrl('http://www.facebook.com/feeds/friends_status.php?id=MY_USER_ID&key=SECRET_KEY&format=rss20')
I am trying to use an rss feed from a domain that does not have a crossdomain file and because of that I am going to use a web service in the middle where I will be just getting the rss feed from a url (let's say the url is: www.example.com/feed) and then just print it to a page.
The service would work like: www.mywebservice.com/feed.php?word=something) and that will just go print the rss feed for: www.example.com/feed&q=word).
I used:
<?php
$word = $_GET["word"];
$ch=curl_init("http://example.com/feed.php?word=".$word."");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
print $data;
?>
But this did not work, it gives me (SYSTEM ERROR: we're sorry but a serious error has occurred in the system). I am on shared hosting
Any help?
The readfile function reads a file and writes it to the output buffer.
readfile('http://example.com/feed.rss');
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the List of Supported Protocols/Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.
If you need to do anything with the XML, use one of PHP's many XML libraries, preferably DOM, but there is also SimpleXml or XMLReader. As an alternative, you could use Zend_Feed from the Zend Framework as a standalone component to work with the RSS feed.
If you cannot enable allow_url_fopen on your server, try cURL like Matchu suggested or go with Artefacto's suggestion.
Consider doing this with mod_rewrite (using the P flag) or setting up a reverse proxy with ProxyPass.
Since you say you can't do the fancy URL-file-opening shortcuts due to server restrictions, you will need to use PHP's cURL module to send an HTTP request.
If you want to also parse XML and process it further, be sure to look into SimpleXML. Let's you parse and manipulate the feed.
So at the end I ended up doing this:
$word = $_GET["word"];
$url = "http://www.example.com/feed.php?q=".$word;
$curl = #curl_init ($url);
#curl_setopt ($curl, CURLOPT_HEADER, FALSE);
#curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE);
#curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, TRUE);
#curl_setopt ($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$source = #curl_exec ($curl);
#curl_close ($curl);
print $source;
I hope this is considered as an answer not an edit (if an edit please tell me so I can just delete this answer and edit the post)