note 13088 deleted from function.fsockopen by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 21:57:08 +0000 |
| Subject: | note 13088 deleted from function.fsockopen by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22254@lists.php.net to get a copy of this message | ||
A snippet of code for pulling pages from servers that use 'chunked' transfer encodings;
i.e. weather.com
Basically, the server breaks the page up into chunks and embeds the size of each chunk in line with
the page, delimited with returns. Some people, seeing this, have thought it is 'garbage'
or 'junk' characters in the page... take a look at the headers the server returns, and
look for chunking.
This code will pull a page off a chunking server, and throw it into an array: $page. It repairs
lines broken by the insertion on the chunk-size codes. There's little/no error-checking; add as
you see fit. You may also want to change the timeout value to match your situation.
---
$url='some page';
$fp=fsockopen('www.weather.com',80);
if ($fp) {
fputs($fp,"GET $url HTTP/1.1\nHost: www.weather.com\nConnection: close\n\n");
socket_set_timeout($fp,2);
$page[0]=''; $chunk=0; $header=0; $html=0;
while (!feof($fp)) {
if ($chunk>0) {
$page[count($page)-1]=chop($page[count($page)-1]);
$line=fread($fp,$chunk);
$temp=explode("\n",$line);
foreach ($temp as $line) $page[]="$line\n";
fgets($fp,2048);
$line=fgets($fp,2048);
$chunk=hexdec(trim($line));
if (!$chunk) break;
continue;
}
else $line=fgets($fp,2048);
if ($header && $html) { $page[]=$line; continue; }
if (!$header) {
if (substr(strtoupper($line),0,18)=='TRANSFER-ENCODING:' &&
stristr($line,'CHUNKED')) $chunk=-1;
elseif (chop($line)=='') $header=1;
continue;
}
if (!$html) {
if (intval(hexdec(trim($line)))) $chunk=hexdec(trim($line));
elseif (substr(trim($line),0,1)=='<') { $page[]=$line; $html=1; }
continue;
}
}
$socket=socket_get_status($fp);
fclose($fp);
}
if (!$socket['timed_out']) {
reset($page)
// go nuts!
}