利用 stream_socket_client 、 fwrite 函数,根据手册上的例子获取域名对应的首页是没问题的,或许内页,始终是 400错误?有没有能做成功的?

解决方案 »

  1.   

    贴你的代码。肯定是HTTP header格式不正确
      

  2.   

    <?php
    $retdata = '';
    $fp = stream_socket_client("www.jxcms.com:80/download/", $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        fwrite($fp, "GET / HTTP/1.0\r\nHost: www.jxcms.com/download/\r\nAccept: */*\r\n\r\n");
        while (!feof($fp)) {
            $retdata .= fgets($fp, 1024);
        }
        fclose($fp);
    }
    $strs = explode("\r\n\r\n", $retdata, 2);
    echo isset($strs[1])?$strs[1]:$retdata;
    ?>
      

  3.   

        fwrite($fp, "GET /download/ HTTP/1.0\r\nHost: www.jxcms.com\r\nAccept: */*\r\n\r\n");
      

  4.   

    改进了下,可以获取了,那个HTTP header抄了fsock方式获取信息的代码,下面的代码请高手指教,是否可以再优化下。<?php
    $retdata = '';
    $tmp = parse_url('http://www.jxcms.com/download/');
    $host = $tmp['host'];
    $path = $tmp['path'];
    empty($tmp['query']) or $path .= '?' . $tmp['query'];
    if (empty($tmp['port'])) {
    $port = $tmp['scheme'] == 'https' ? 443 : 80;
    } else $port = $tmp['port'];
    $fp = stream_socket_client("$host:$port", $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $tmp = "GET $path HTTP/1.1\r\n";
    $tmp .= "Host: $host\r\n";
    $tmp .= "User-Agent: $user_agent\r\n";
    $tmp .= "Connection: Close\r\n\r\n";
    fwrite($fp, $tmp);
        while (!feof($fp)) {
            $retdata .= fgets($fp, 1024);
        }
        fclose($fp);
    }
    $strs = explode("\r\n\r\n", $retdata, 2);
    echo isset($strs[1])?$strs[1]:$retdata;
    ?>
      

  5.   

    $context = stream_context_create( array( 'header'=>
                         array('User-Agent'=>$_SERVER['HTTP_USER_AGENT']) 
                        )
    );
    echo file_get_contents('http://www.jxcms.com/download/', false, $context);如果要获取header报头 可以参看:  $http_response_header 预定义局部变量
      

  6.   

    sorry stream_context_create()的参数格式我给弄错了
    $options = array(
      'http'=> array( 'header'=> 
                   array("User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n") 
            )
    );
    $context = stream_context_create($options);