Page 1 of 1
how to get external XML?
Posted: Tue Jul 26, 2005 4:44 am
by saumya
Hi people,
I am trying to read an RSS.but my server does not shows me anything, neither an error nor any display.
Code: Select all
<?php
$source ="http://developers.sun.com/rss/sdn.xml";
$stats = file_get_contents($source);
echo "$stats";
?>
any ehlp will be a great help.
Posted: Tue Jul 26, 2005 4:59 am
by anjanesh
Thats because the tags are being parsed in your browser. <title></title>, <description></description> etc which are invalid HTML tags but still caught.
It works alright.
Code: Select all
<pre>
<?php
$source ="http://developers.sun.com/rss/sdn.xml";
$stats = htmlentities(file_get_contents($source));
echo "$stats";
?>
</pre>
When you are actually parsing the tags remove the htmlentities() part :
$stats = file_get_contents($source);
Posted: Tue Jul 26, 2005 5:07 am
by saumya
Thank you very much.
But it is not working for me.In my server its not displaying anything.I am under a proxy and running my own apache in winxp.It should show me the dialogue box to connect to the external site,I guess.is not it?it actually does not showing me anything.
Anyhelp
Posted: Tue Jul 26, 2005 5:31 am
by anjanesh
Dont know how it works on a proxy but many hosts have disbled remote file connections directly. IF CURL Library is installed and configured you can try this :
Replace file_get_contents($source) with emulate_file_get_contents($source)
and add this function :
Code: Select all
function emulate_file_get_contents($url)
{
$handle = curl_init();
curl_setopt ($handle, CURLOPT_URL, $url);
curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1);
$file_contents = curl_exec($handle);
curl_close($handle);
return $file_contents;
}
Posted: Tue Jul 26, 2005 5:45 am
by saumya
i do not think curl is installed.It does not show me in my phpinfo.Anyway great help.Thanks
is there anyother way i can get access to external xml
Posted: Tue Jul 26, 2005 6:58 am
by timvw
The following code works perfectly overhere...
Code: Select all
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
$source ="http://developers.sun.com/rss/sdn.xml";
$stats = file_get_contents($source);
header('Content-type: text/xml');
echo "$stats";
?>
Posted: Tue Jul 26, 2005 7:01 am
by anjanesh
timvw wrote:
The following code works perfectly overhere...
When over a proxy, is it possible that the port may not be 80 ?
Posted: Tue Jul 26, 2005 7:02 am
by timvw
That's why i added the error_reporting... It would assure the OP can see the warnings/errors from PHP

Posted: Tue Jul 26, 2005 7:11 am
by saumya
thank guys.I got something in my browser.it says
Warning: file_get_contents(
http://developers.sun.com/rss/sdn.xml) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in D:\Saumya\PHP\ExternalXml_RSS\JavaForumTest.php on line 7
Fatal error: Maximum execution time of 30 seconds exceeded in D:\Saumya\PHP\ExternalXml_RSS\JavaForumTest.php on line 7
Posted: Tue Jul 26, 2005 7:17 am
by timvw
You might want to try if you can download the file from that computer with a regular browser.. (This way we know the problem is with the apache/php config or a more general one)
Posted: Tue Jul 26, 2005 7:24 am
by anjanesh
Add to
timvw code set_time_limit (0); in the beginning.
Posted: Tue Jul 26, 2005 7:29 am
by saumya
i could not got you ?
i can download from anysite in this comp.