zoukankan      html  css  js  c++  java
  • 模拟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;
    }
    ?>
  • 相关阅读:
    StringUtils工具类的使用
    struts2 文件上传和下载,以及部分源代码解析
    ios开发之猜数字游戏
    从epoll构建muduo-12 多线程入场
    POJ3009 Curling 2.0(DFS)
    IOS-4-面试题1:黑马程序猿IOS面试题大全
    Android-Universal-Image-Loader载入图片
    《UNIX环境高级编程》读书笔记 —— 文件 I/O
    畅通project再续 HDU杭电1875 【Kruscal算法 || Prim】
    轻松学习之Linux教程四 神器vi程序编辑器攻略
  • 原文地址:https://www.cnblogs.com/lexus/p/2377287.html
Copyright © 2011-2022 走看看