Page 1 of 1

read a remote xml file and display the xml file

Posted: Sat Aug 20, 2005 10:23 pm
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.

Posted: Sat Aug 20, 2005 10:28 pm
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.

Posted: Sun Aug 21, 2005 2:44 am
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)

Posted: Sun Aug 21, 2005 7:43 am
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.

Posted: Sun Aug 21, 2005 9:40 am
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?

Posted: Sun Aug 21, 2005 9:44 am
by feyd
did you try file_get_contents() ?

Posted: Sun Aug 21, 2005 10:15 am
by BMN

Code: Select all

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