我构建了http(post)请求,发出后,我想获取http反应回来的头部信息

解决方案 »

  1.   

    $username=$_POST['username'];
      $password=$_POST['password'];
       $host="www.teamsun.com.cn:88";
      $service_uri="/login.msc";
       $query_string="user=".$username."&password=".$password."&mode=submit";
       $http_request="POST ". $service_uri ." HTTP/1.1\r\n";
       $http_request.="Host:$host\r\n";
       $http_request.="Content-Type: application/x-www-form-urlencoded\r\n";
       $http_request.="Accept-Encoding: gzip, deflate\r\n";
       $http_request.="Content-Length:".strlen($query_string)."\r\n";
      $http_request.="Connection: Keep-Alive\r\n";
       $http_request.="Cache-Control: no-cache\r\n\r\n";
       $http_request.=$query_string."\r\n\r\n";
      
      
       $fs = fsockopen($host,88);
      if (!is_resource($fs)){
          echo "trackback_send: Couldn't connect to $host,Port 88"; 
           return false;
         }
         fwrite($fs, $http_request);
         while(!feof($fs))
         {
         $response.=fread($fs,4096);
         }
        fclose($fs);
        $headers = getallheaders();
         while (list($header, $value) = each($headers)) {
          echo "$header: $value<br>\n";
         }
    打印的为空值
      

  2.   

    fwrite 后. 跟着用 
    用 fgets 读取数据得到返回结果.    while (!feof($fp))
        {
            $line.=fgets($fp, 512);
                
      

  3.   

    楼上的这没关系吧,fgets碰都\r\n就结束了
      

  4.   

    $host="www.teamsun.com.cn:88";
    ==============================
    你确定host是这样的?怎么还带端口号?
      

  5.   

    这个没关系,是不是由于getallheaders (),他是获取每个请求的http头部信息,那如何取得返回来的
      

  6.   

    肯定是 getallheader 这个的问题.
    有好好的 curl 为什么不用?
      

  7.   

    ....
    ...
    ..
    ..
    echo $headers;//测试一下有无头信息输出?有,则把函数getallheaders贴出来看看。
    $headers = getallheaders();
    ..
    .
    ..
    .
      

  8.   

    sorry,应该是
    ==============
    echo $response//测试一下有无头信息输出?有,则把函数getallheaders贴出来看看。
      

  9.   

    他是php内部函数呀,echo $response打印的是请求的头部信息
      

  10.   

    哪位高手可以指点下呀,我是个php编程新手
      

  11.   

    ^_^,还真没用过这函数。
    那你用正则抓$response里的内容不得了?
      

  12.   

    getallheaders 不是这样用的.....
      

  13.   

    $response打印也为空值
    ==============================================
    这个.............你运行你的程序没有错误提示吗?
      

  14.   

    那你就得调试一下到底哪里出了问题了
    ========================================
    $fs = fsockopen($host,88) or die('无法连接');
      

  15.   

    getallheaders是用来获取用户请求头数据的...
    用curl多简单啊 为什么要自己写socket...