模拟http请求,支持gzip,chunked格式

模拟http请求,支持gzip,chunked格式 - rains的日志 - PHPChina

模拟http请求,支持gzip,chunked格式

已有 1215 次阅读2009-3-2 14:06

|

0

<?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";
    $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&lt;$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;
}
?>
posted on 2012-03-02 15:30  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/03/02/2377287.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值