Chinaunix

标题: php socket模拟http请求返回乱码 [打印本页]

作者: tanos    时间: 2008-10-10 15:26
标题: php socket模拟http请求返回乱码
rt
代码如下,不明白原因,麻烦大家指正一下

$httphead = <<<eof
GET /index.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; (R1 1.5); .NET CLR 1.1.4322)
Host: aa.aa.com\r\n\r\n
Connection: Keep-Alive \r\n\r\n
eof;
$length = strlen($httphead);
$sock = fsockopen("www.baidu.com",80);
fwrite($sock,$httphead,$length);
while (!feof($sock))
echo fgets($sock);

作者: wjjchen    时间: 2008-10-10 17:17
header信息里面每行加入\r\n试试
作者: vinchen    时间: 2008-10-10 17:36
手册上就有个很好的例子

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";

    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128 ) ;
    }
    fclose($fp);
}
?>

[ 本帖最后由 vinchen 于 2008-10-10 17:38 编辑 ]
作者: sunceenjoy    时间: 2008-10-13 10:33
说不定页面被zip过了。解压吧.
作者: tanos    时间: 2008-10-13 11:26
标题: 回复 #4 sunceenjoy 的帖子
请教一下,如何解压?谢谢
作者: lastexile    时间: 2008-10-13 15:30
用php的Snoopy的类吧, 何必自己构造这么辛苦
作者: sunceenjoy    时间: 2008-10-16 09:46
要是我说的那种情况的话,有个什么phpzip类可以调用或者exec('gzip -d get.html');的返回值
作者: wjjchen    时间: 2011-03-25 10:12
去掉这行就可以了。
Accept-Encoding: gzip, deflate
作者: ws00377531    时间: 2011-03-25 13:41
找个没有gzip的网站测试一下啊




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2