|
Posted by Andy Hassall on 11/25/06 14:15
On 25 Nov 2006 05:53:54 -0800, saddor@gmail.com wrote:
>Solution One
><?php
>$host = "www.example.com";
>$fp = fsockopen($host, 80, $errno, $errstr, 30);
>if (!$fp) {
> echo "$errstr ($errno)<br />\n";
>} else {
> $out = "GET / HTTP/1.1\r\n";
> $out .= "Host: $host\r\n";
> $out .= "Connection: Close\r\n\r\n";
>
> fwrite($fp, $out);
> while (!feof($fp)) {
> echo fgets($fp, 128);
> }
> fclose($fp);
>}
>?>
This has a common but serious mistake - you're claiming to be an HTTP/1.1
client, but you haven't implemented Chunked transfer encoding. This is a
mandatory requirement for HTTP/1.1 clients, and it's quite common that servers
use it. If you don't handle it, the data will appear to be "corrupted".
The other solutions you posted use PHP's built-in HTTP user agent code, which
does handle this encoding correctly. Alternatively the code above could be
changed to use HTTP/1.0.
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Navigation:
[Reply to this message]
|