由于目标服务器响应时将错误代码存在head中,有什么办法可以获取head吗?我是用file_get_contents函数发送的请求。
下面是发送请求的代码,随便从文档中粘了段,我的代码也和这个差不多。
怎样才能获取到返回的head信息?<?php$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);$context  = stream_context_create($opts);$result = file_get_contents('http://example.com/submit.php', false, $context);?> 

解决方案 »

  1.   

    1.用sniffer等网络嗅探器检测
    2.用CURL获取head的代码如下:
       curl_setopt($ch,CURLOPT_HEADER,1);
      

  2.   

    get_headers函数
    <?php
    $url = 'http://www.example.com';print_r(get_headers($url));print_r(get_headers($url, 1));
    ?>
    输出结果:
    [code=BatchFile]Array
    (
        [0] => HTTP/1.1 200 OK
        [1] => Date: Sat, 29 May 2004 12:28:13 GMT
        [2] => Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux)
        [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
        [4] => ETag: "3f80f-1b6-3e1cb03b"
        [5] => Accept-Ranges: bytes
        [6] => Content-Length: 438
        [7] => Connection: close
        [8] => Content-Type: text/html
    )Array
    (
        [0] => HTTP/1.1 200 OK
        [Date] => Sat, 29 May 2004 12:28:14 GMT
        [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
        [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
        [ETag] => "3f80f-1b6-3e1cb03b"
        [Accept-Ranges] => bytes
        [Content-Length] => 438
        [Connection] => close
        [Content-Type] => text/html
    )
    [/code]
      

  3.   

    用楼上的代码的话会产生一个问题,得发送2次请求,一次获得head,一次获得正文内容。可目标服务器对同一用户有请求限额,每日不得超过XXXXXX次。有没有什么办法只发送一次请求就能都获得的。curl函数可以实现吗?
      

  4.   

    f_get();
    function f_get()
    {
    $host = "www.7qb.cn";
    $file = "/";
    $fp = fsockopen( $host , 80, $errno, $errstr, 30 );
    if (!$fp) {
    echo "$errstr ($errno)<br />\n";
    } else {
    $request = "GET $file HTTP/1.1\r\n";
    $request .= "Accept: */*\r\n";
    $request .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)\r\n";
    $request .= "Host: $host\r\n";
    //$request .= "Range: bytes=1321678-\r\n";
    $request .= "Connection: close\r\n";
    $request .= "\r\n"; fwrite($fp, $request);
    while (!feof($fp)) {
    echo fgets($fp, 128);
    //这里获取数据
    if ($i++>9)
    {
        break;
    }
    }
    fclose($fp);
    }
    }
      

  5.   


    以为你只想要投信息。向服务器发送请求了,肯定是要返回头信息的。
    但是,这需要HTTP客户端功能,file_get_contents不是专门用来发送HTTP请求的,不会有这些功能。
    PHP实现HTTP客户功能的方法很多,
    推荐lz使用: Zend_HTTP操作起来很方便。
      

  6.   

    我用firefox的firebug获取header信息非常好用啊!