how to get external XML?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

how to get external XML?

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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);
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post 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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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;
 }
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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";
?>
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

timvw wrote: The following code works perfectly overhere...
When over a proxy, is it possible that the port may not be 80 ?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

That's why i added the error_reporting... It would assure the OP can see the warnings/errors from PHP ;)
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Add to timvw code set_time_limit (0); in the beginning.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

i could not got you ?
i can download from anysite in this comp.
Post Reply