read a remote xml file and display the xml file

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
BMN
Forum Newbie
Posts: 15
Joined: Fri Aug 12, 2005 10:54 pm

read a remote xml file and display the xml file

Post by BMN »

I need to read a remote xml files and just print the remote xml file as it is ont he browser.

I know there is a built in function in php5 but I am using php4.

I do not need to parse it. I just need to pass this to my javascript xml parser since when I try to read a remote xml file using

Code: Select all

requester.open("GET", "http://slashdot.org/slashdot.xml");
i get a security error message.

I would really appreciate any help.
Last edited by BMN on Sat Aug 20, 2005 10:28 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

built-in file request have been in for quite some time, I'm not sure what you're referring to that's in 5, but not in 4 for this that really matters..

file_get_contents() can request an external file (provided url wrappers are turned on). Display of it is quite simple, depending on how you want to show it.. you could simply send a text/plain header to the browser and the found text, or use htmlentities() to display the code inside pretty stuff.
BMN
Forum Newbie
Posts: 15
Joined: Fri Aug 12, 2005 10:54 pm

Post by BMN »

All i want to do is read the xml files from a web site and echo it to my browser

i tried the below code

Code: Select all

$fp = fsockopen("http://slashdot.org/slashdot.xml", 80, $errno, $errstr, 30);
if (!$fp) {
   echo "$errstr ($errno)<br />\n";
} else {
   $out = "GET / HTTP/1.1\r\n";
   $out .= "Host: www.example.com\r\n";
   $out .= "Connection: Close\r\n\r\n";

   fwrite($fp, $out);
   while (!feof($fp)) {
       echo fgets($fp, 128);
   }
   fclose($fp);
}
but keep getting the below error

Code: Select all

No such file or directory (2)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You get the error because you don't have a clue what fsockopen is really doing.. It would require you to open a socket on slashdot.org and then fwrite a GET /slashdot.xml...

But as feyd already mentionned, simply use file_get_contents / file (with the HTTP(S) wrappers enabled) so you don't have to write the HTTP stuff yourself.

If you are going to server the data from the xml feed, you should make sure that you send out the appropriate Content-Type header too. Because i've seen XMLHTTP choke on that.
BMN
Forum Newbie
Posts: 15
Joined: Fri Aug 12, 2005 10:54 pm

Post by BMN »

Code: Select all

header("Content-type: text/xml");

$fp = fsockopen("slashdot.org", 80, &$errno, &$errstr, 30);
if(!$fp) {
	echo "$errstr ($errno)<br>\n";
} else {
	fputs($fp,"GET /slashdot.xml\n\n");
	while(!feof($fp)) {
		echo fgets($fp,128);
	}
	fclose($fp);
}
I get the below error

Code: Select all

HTTP/1.0 501 Not Implemented Content-Type: text/html Content-Length: 136
501 Not Implemented

This method may not be used.
Can someone please help me with this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you try file_get_contents() ?
BMN
Forum Newbie
Posts: 15
Joined: Fri Aug 12, 2005 10:54 pm

Post by BMN »

Code: Select all

$xmlDoc = xmldocfile($websiteURL);
if ($xmlDoc){
    print $xmlDoc->dumpmem();
}
worked
Post Reply