代码之家  ›  专栏  ›  技术社区  ›  Alon Gubkin

改进HTTP GET PHP脚本

  •  0
  • Alon Gubkin  · 技术社区  · 15 年前

    此代码从$url获取标题和内容,并将其打印到浏览器。这真的很慢,不是因为服务器。我该如何改进?

    $headers = get_headers($url);
    
    foreach ($headers as $value)
        header($value);
    
    $fh = fopen($url, "r");
    fpassthru($fh);
    

    谢谢

    5 回复  |  直到 15 年前
        1
  •  1
  •   GZipp    15 年前

    为什么要提出两个请求,而一个会这样做?

    $fh = fopen($url, 'r');
    foreach ($http_response_header as $value) {
        header($value);
    }
    fpassthru($fh);
    

    或者:

    $content = file_get_contents($url);
    foreach ($http_response_header as $value) {
        header($value);
    }
    echo $content;
    
        2
  •  0
  •   Myles    15 年前

    我不知道你为什么要在第6行打开一个连接,如果你已经有并打印出了标题。这是否不仅仅是打印出邮件头?

        3
  •  0
  •   jheddings    15 年前

    如果您真的只想代理一个页面,那么curl函数的效率要高得多:

    <?
      $curl = curl_init("http://www.google.com");
      curl_setopt($curl, CURLOPT_HEADER, true);
      curl_exec($curl);
      curl_close($curl);
    ?>
    

    当然,curl必须在服务器上启用,但这并不少见。

        4
  •  0
  •   sanmai    15 年前

    你想做代理吗?如果是这样,下面是proxy.php中的一个配方:

    <?php
    $host = 'example.com';
    $port = 80;
    
    $page = $_SERVER['REQUEST_URI'];
    
    $conn = fsockopen($host, $port, $errno, $errstr, 180);
    if (!$conn) throw new Exception("$errstr ($errno)");
    
    $hbase = array();
    $hbase[] = 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
    if (!empty($_SERVER['HTTP_REFERER'])) $hbase[] = 'Referer: '.str_ireplace($_SERVER['HTTP_HOST'], $host, $_SERVER['HTTP_REFERER']);
    if (!empty($_SERVER['HTTP_COOKIE'])) $hbase[] = 'Cookie: '.$_SERVER['HTTP_COOKIE'];
    $hbase = implode("\n", $hbase);
    
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $post = file_get_contents("php://input");
        $length = strlen($post);
        $request = "POST $page HTTP/1.0\nHost: $host\n$hbase\nContent-Type: application/x-www-form-urlencoded\nContent-Length: $length\n\n$post";
    } else $request = "GET $page HTTP/1.0\nHost: $host\n$hbase\n\n";
    
    do {
        $conn = fsockopen($host, 80, $errno, $errstr, 180);
        if (!$conn) throw new Exception("$errstr ($errno)");
    
        fputs($conn, $request);
    
        $header = false;
        $body = false;
    
        stream_set_blocking($conn, false);
        $info = stream_get_meta_data($conn);
        while (!feof($conn) && !$info['timed_out']) {
            $str = fgets($conn);
            if (!$str) {
                usleep(50000);
                continue;
            }
    
            if ($body !== false) $body .= $str;
            else $header .= $str;
    
            if ($body === false && $str == "\r\n") $body = '';
            $info = stream_get_meta_data($conn);
        }
        fclose($conn);
    } while ($info['timed_out']);
    
    $header = str_ireplace($host, $_SERVER['HTTP_HOST'], $header);
    if (stripos($body, $host) !== false) $body = str_ireplace($host, $_SERVER['HTTP_HOST'], $body);
    
    $header = str_replace('domain=.example.com; ', '', $header);
    
    $header_array = explode("\r\n", $header);
    foreach ($header_array as $line) header($line);
    
    if (strpos($header, 'Content-Type: text') !== false) {
        $body = str_replace('something', '', $body);
    }
    
    echo $body;
    

    在HTAccess中:

    Options +FollowSymlinks
    
    RewriteEngine on
    RewriteBase /
    RewriteRule ^(.*)$ proxy.php [QSA,L]
    
        5
  •  0
  •   Tim Lytle    15 年前

    您可以通过将$url更改为已知的快速站点,甚至本地Web服务器来确定速度慢。唯一可能的事情是服务器的响应缓慢。

    当然,正如gzip建议的那样,如果您也要输出文件内容,只需使用一个请求就可以了。这会让你请求的服务器更快乐。

    推荐文章