<?php
$host = "www.123cha.com";
$referer = "http://".$host;
$fp = fsockopen ($host, 80, $errno, $errstr, 30);
if (!$fp){
        echo "$errstr ($errno)<br>;n";
}else{
$request = "
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
Referer: http://$host
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: $host
Connection: Close"
."rnrn";
fputs ($fp, "$request");
while (!feof($fp))
{
   $res[] = fgets($fp,1024);
}
$html = join("",$res);
fclose ($fp);
$fp = file_put_contents("123cha.html",$html);
echo "done";
}
谁能告诉我这段代码每步都是什么意思?谁能写下注释啊,小弟看不懂,正在研究PHP模拟浏览器请求就查到此代码

解决方案 »

  1.   

    <?php
    $host = "www.123cha.com";      //定义变量
    $referer = "http://".$host;  //定义变量,并连接上一个串
    $fp = fsockopen ($host, 80, $errno, $errstr, 30); // sock 到指定地址(即上面那个)。
    if (!$fp){
            echo "$errstr ($errno)<br>;n"; //返回空,输出信息
    }else{
    $request = "
    GET / HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
    Referer: http://$host
    Accept-Language: zh-cn
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    Host: $host
    Connection: Close"
    ."rnrn";//构造一个get
    fputs ($fp, "$request");//向server  发送get请求。
    while (!feof($fp))
    {
       $res[] = fgets($fp,1024); //记录返回的信息。
    }
    $html = join("",$res);//将返回数组连成一个串。
    fclose ($fp); //关闭 socket
    $fp = file_put_contents("123cha.html",$html);  //保存串。
    echo "done"; //告诉你,嘿,完成了
    }
      

  2.   

    fputs ($fp, $request);
    这样就可以了。
      

  3.   


    $host = "www.123cha.com"; //网址
    $referer = "http://".$host; //浏览器返回(跳转)地址
    $fp = fsockopen ($host, 80, $errno, $errstr, 30);//打开一个套接字,网络数据传输
    if (!$fp){//如果没有打开
            echo "$errstr ($errno)<br>;n";
    }else{//如果打开成功
    $request = "
    GET / HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
    Referer: http://$host
    Accept-Language: zh-cn
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    Host: $host
    Connection: Close"
    ."rnrn";//头信息
    fputs ($fp, "$request");//向浏览器写信息(写出后,请求也会随之发送)
    while (!feof($fp)){//如果还有需要写的接着写,一直到没有为止
       $res[] = fgets($fp,1024);
    }
    $html = join("",$res);//连接字符串
    fclose ($fp);//关闭套接字连接
    $fp = file_put_contents("123cha.html",$html);//写入文件
    echo "done";
      

  4.   

    <?php
    $host = "www.123cha.com";
    $referer = "http://".$host;
    //打开www.123cha.com的socket连接
    $fp = fsockopen ($host, 80, $errno, $errstr, 30);
    if (!$fp){
    //如果打开失败,输出错误信息
    echo "$errstr ($errno)<br>;n";
    }else{
    $request = "
    GET / HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
    Referer: http://$host
    Accept-Language: zh-cn
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    Host: $host
    Connection: Close"
    ."rnrn";
    // 发送上面的request内容给www.123cha.com
    fputs ($fp, "$request");
    while (!feof($fp)){
    //循环获取网站的返回内容,就是response
    $res[] = fgets($fp,1024);
    }
    //将返回的内容拼接成HTML文档。
    $html = join("",$res);
    //关闭socket连接
    fclose ($fp);
    //将网站返回的内容写入文件123cha.html
    $fp = file_put_contents("123cha.html",$html);
    echo "done";
    }
    ?>
      

  5.   

    主要就是获取 123cha.com 的查询结果。然后保存为本地网页文件 123cha.html
      

  6.   

    我来解释这段:
    $request = "
    GET / HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
    Referer: http://$host
    Accept-Language: zh-cn
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    Host: $host
    Connection: Close"
    ."\r\n\r\n";GET / HTTP/1.1
    get方式,遵循 http 1.1 协议Accept: 
    允许列表中的数据类型被返回Referer: http://$host
    模拟来路,以免访问被拒绝Accept-Language: zh-cn
    使用的语言Accept-Encoding: gzip, deflate
    支持压缩方式的返回数据。其实这条不应该写,如果对方真的以压缩方式返回数据的话,不是还有个解压缩的麻烦吗?User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    把自己收成是ie6浏览器Host: $host
    目标服务器地址Connection: Close"
    通知服务器,回传数据后关闭连接
      

  7.   


    Warning: fsockopen() [function.fsockopen]: unable to connect to http://zh.wikipedia.org/zh-cn/Special:%CB%E6%BB%FA%D2%B3%C3%E6:8080 (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in D:\WebSite\htdocs\Aih\Browser.class.php on line 8
    Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?
    我用了以上写法 报了这个错误该怎么办
      

  8.   

    php.ini中的fsockopen对应的.dll没开启
      

  9.   


    ;extension=php_sockets.dll前的‘;’去掉
      

  10.   

    这条是不是可以接收到新的地址?还有个问题上面的比如Appect我可以不写么?我只想获取到跳转后的地址该怎么写
      

  11.   


    function returnUrl($host)//$host 这里传进来的是这个网址http://zh.wikipedia.org/zh-cn/Special:随机页面
    {

    $referer = "http://".$host;  //定义变量,并连接上一个串
    $fp = fsockopen ($host, 8080, $errno, $errstr, 30); // sock 到指定地址(即上面那个)。
    if (!$fp){
            echo "$errstr ($errno)<br>;n"; //返回空,输出信息
    }else{
    $request = "
    GET / HTTP/1.1
    Referer: http://$host
    Accept-Language: zh-cn
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    Host: $host
    Connection: Close"
    ."rnrn";

    //构造一个get
    fputs ($fp, "$request");//向server  发送get请求。
    while (!feof($fp))
    {
       $res[] = fgets($fp,1024); //记录返回的信息。
    }
    $html = join("",$res);//将返回数组连成一个串。
    fclose ($fp); //关闭 socket
    $fp = file_put_contents("url.html",$html);  //保存串。
    return $fp;

    }
    }
    看上面的代码哪里有问题 我还是报一下错误Warning: fsockopen() [function.fsockopen]: unable to connect to http://zh.wikipedia.org/zh-cn/Special:随机页面:8080 (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in D:\WebSite\htdocs\Aih\Browser.class.php on line 8
    Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? (32)
    ;n
      

  12.   

    $url = "http://zh.wikipedia.org/zh-cn/Special:%CB%E6%BB%FA%D2%B3%C3%E6:8080";
    print_r(get_headers($url));这是该url返回的所有标头
    Array
    (
        [0] => HTTP/1.0 403 Forbidden
        [1] => Date: Tue, 20 Jul 2010 09:59:59 GMT
        [2] => Server: Apache
        [3] => Cache-Control: no-cache
        [4] => Vary: Accept-Encoding
        [5] => Content-Length: 120
        [6] => Content-Type: text/html
        [7] => X-Cache: MISS from sq63.wikimedia.org
        [8] => X-Cache-Lookup: MISS from sq63.wikimedia.org:3128
        [9] => X-Cache: MISS from sq60.wikimedia.org
        [10] => X-Cache-Lookup: MISS from sq60.wikimedia.org:80
        [11] => Connection: close
    )
      

  13.   

    有位高人 学习 HTTP报头