Re: Reading in contents of URL
| From: | Richard Lynch | Date: | Mon, 05 Aug 2002 06:00:48 +0000 |
| Subject: | Re: Reading in contents of URL | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-111157@lists.php.net to get a copy of this message | ||
>1. read in the contents of a URL.
>2. get the content-type
>3. set the content type for the response
>4. write the contents of the URL to the response.
>
>here is what i have thus far:
>
>$dataURL = "http://www.foo.com/data.xml";
>$fp = fopen($dataURL, "r");
>$content = fread($fp, true);
>echo($content);
>fclose($fp);
>
>I am running into two problems. One, the second param for fread takes
>the size of the file / content. how do i get this?
You don't.
Actually, if you can get the headers, and *IF* the headers are kosher enough
to tell you the Content-length:, and if that number is actually correct, you
would know how many bytes.
*BUUUUUT* I sure wouldn't trust every web-server on the planet to correctly
output the Content-length: anyway, so that's out.
Your choices are:
1. Use http://php.net/eof and a loop to figure out when
there's no more HTML
coming.
2. Assume every URL has a reasonable amount of data and just use http://file
>there is a comment at:
>http://www.php.net/manual/en/function.fread.php
>
>which says to just specify a constant and it will work, but what ever
>number i specify, that is the amount of data that is read.
>
>how do i retrieve the content type?
Assuming you're not running 4.3.0 from CVS and
http://www.php.net/manual/en/function.file_get_wrapper_data.php
is not
helpful, you're stuck with doing something like this:
<?php
$sock = fsockopen('example.com', 80) or die("Could not open socket to
example.com");
fputs($sock, "GET /whatever.html HTTP/1.0"); # Might need \n on the end...
fputs($sock, "Host: example.com"); # In case it's virtual hosting.
fputs($sock, ""); # MUST send blank line before output will occur!
# The end of headers is indicated by a blank line:
$inheaders = true;
$headers = '';
while ($inheaders && !feof($sock)){
$line = fgets($sock, 4096); # Any header over 4K in length will screw
this up...
if ($line){
$headers .= $line;
# You could also parse the headers looking for Content-type: and so
on.
# But you should *NEVER* assume that other stupid web-server is going
to bother to tell you...
# Much less tell you correctly.
# If it doesn't say, it's supposed to be text/plain...
# But stupid IE looks at the URLs and uses them anyway, so Good Luck!
}
else{
$inheaders = false;
}
}
$content = '';
while (!feof($sock)){
$content .= fread($sock, 4096); # If you expect to suck down HUGE pages,
increase this...
}
?>
--
Like Music? http://l-i-e.com/artists.htm
I'm looking for a PRO QUALITY two-input sound card supported by Linux (any
major distro). Need to record live events (mixed already) to stereo
CD-quality. Soundcard Recommendations?
Software to handle the recording? Don't need fancy mixer stuff. Zero (0)
post-production time. Just raw PCM/WAV/AIFF 16+ bit, 44.1KHz, Stereo
audio-to-disk.