<?php
/**
* 模拟http请求,支持gzip,chunked格式
*
*/
function http_request($url)
{
$urlinfo = parse_url($url);
$urlinfo['path'] = $urlinfo['path']!=''?$urlinfo['path']:'/';
$header = "GET {$urlinfo['path']} HTTP/1.1/r/n";
$header.= "Accept: */*/r/n";
$header.= "Accept-Language: zh-cn/r/n";
$header.= "UA-CPU: x86/r/n";
$header.= "Accept-Encoding: gzip, deflate /r/n";
$header.= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; .NET CLR 2.0.50727; CIBA)/r/n";
$header.= "Host: {$urlinfo['host']}/r/n";
$header.= "Connection: close /r/n";
$header.= "/r/n";
$header.= "/r/n";
/**
* 模拟http请求,支持gzip,chunked格式
*
*/
function http_request($url)
{
$urlinfo = parse_url($url);
$urlinfo['path'] = $urlinfo['path']!=''?$urlinfo['path']:'/';
$header = "GET {$urlinfo['path']} HTTP/1.1/r/n";
$header.= "Accept: */*/r/n";
$header.= "Accept-Language: zh-cn/r/n";
$header.= "UA-CPU: x86/r/n";
$header.= "Accept-Encoding: gzip, deflate /r/n";
$header.= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; .NET CLR 2.0.50727; CIBA)/r/n";
$header.= "Host: {$urlinfo['host']}/r/n";
$header.= "Connection: close /r/n";
$header.= "/r/n";
$header.= "/r/n";
$fp = fsockopen($urlinfo['host'],isset($urlinfo['port'])?$urlinfo['port']:80,$errno,$errstr,10);
if(!$fp)
{
echo $errno.$errstr;
return false;
}
fwrite($fp,$header);
$head = read_header($fp);
$content = read_content($fp,$head);
return $content;
}
function read_header($fp)
{
$header = '';
while(trim($line=fgets($fp,1024))!="")
{
$header .= $line;
}
return $header;
}
function read_content($fp,$head='')
{
if(!strpos($head,'200 OK'))
{
return false;
}
$content = '';
//只有chunked才要分段处理
if(strpos($head,'chunk'))
{
$chunk_size = chop(fgets($fp,1024));
$chunk_size = hexdec($chunk_size);
$block_size = 0;
while(!feof($fp))
{
//$line = fread($fp,$chunk_size);
//die($chunk_size.":".strlen($line).':'.bin2hex($line));break;
//输出 7873:2360 本来fread应该读7873,结果只读了2360
if($block_size<$chunk_size)
{
$content .= fgetc($fp);
$block_size++;
}
else
{
echo fread($fp,2);
$chunk_size = hexdec(chop(fgets($fp,1024)));
$block_size = 0;
//echo "*****chunk_size:$chunk_size*****";
if($chunk_size==0)
{
fclose($fp);break;
}
}
}
}
else
{
//普通代码普通对待
while(!feof($fp))
{
$content .= fgetc($fp);
}
}
//经过n次测试,不用临时文件还是不行啊。搞不懂。
$tmpfile = tempnam('/tmp','webcache');
$fp = fopen($tmpfile,'w');
fwrite($fp,$content);
fclose($fp);
ob_start();
readgzfile($tmpfile);
$content = ob_get_contents();
ob_end_clean();
unlink($tmpfile);
return $content;
}
?>
if(!$fp)
{
echo $errno.$errstr;
return false;
}
fwrite($fp,$header);
$head = read_header($fp);
$content = read_content($fp,$head);
return $content;
}
function read_header($fp)
{
$header = '';
while(trim($line=fgets($fp,1024))!="")
{
$header .= $line;
}
return $header;
}
function read_content($fp,$head='')
{
if(!strpos($head,'200 OK'))
{
return false;
}
$content = '';
//只有chunked才要分段处理
if(strpos($head,'chunk'))
{
$chunk_size = chop(fgets($fp,1024));
$chunk_size = hexdec($chunk_size);
$block_size = 0;
while(!feof($fp))
{
//$line = fread($fp,$chunk_size);
//die($chunk_size.":".strlen($line).':'.bin2hex($line));break;
//输出 7873:2360 本来fread应该读7873,结果只读了2360
if($block_size<$chunk_size)
{
$content .= fgetc($fp);
$block_size++;
}
else
{
echo fread($fp,2);
$chunk_size = hexdec(chop(fgets($fp,1024)));
$block_size = 0;
//echo "*****chunk_size:$chunk_size*****";
if($chunk_size==0)
{
fclose($fp);break;
}
}
}
}
else
{
//普通代码普通对待
while(!feof($fp))
{
$content .= fgetc($fp);
}
}
//经过n次测试,不用临时文件还是不行啊。搞不懂。
$tmpfile = tempnam('/tmp','webcache');
$fp = fopen($tmpfile,'w');
fwrite($fp,$content);
fclose($fp);
ob_start();
readgzfile($tmpfile);
$content = ob_get_contents();
ob_end_clean();
unlink($tmpfile);
return $content;
}
?>