tags:

views:

128

answers:

5

This code is getting the headers and content from $url, and prints it to the browser. It is really slow, and it's not because the server. How can I improve this?

$headers = get_headers($url);

foreach ($headers as $value)
    header($value);

$fh = fopen($url, "r");
fpassthru($fh);

Thanks

A: 

I'm not sure why you're opening a connection there on line 6 if you already have and have printed out the headers. Is this doing more than printing out headers?

Myles
+1  A: 

Why make two requests when one will do?

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

Or:

$content = file_get_contents($url);
foreach ($http_response_header as $value) {
    header($value);
}
echo $content;
GZipp
A: 

If you are really looking to just proxy a page, the cURL functions are much more efficient:

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

Of course, cURL has to be enabled on your server, but it's not uncommon.

jheddings
A: 

Are you trying to make a proxy? If so, here is a recipe, in 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;

In .htaccess:

Options +FollowSymlinks

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ proxy.php [QSA,L]
sanmai
A: 

You may be able to pinpoint the slowness by changing $url to a know fast site, or even a local webserver. The only thing that seems to be possible is a slow response from the server.

Of course as suggested by GZipp, if you're going to output the file contents as well, just do it with a single request. That would make the server you're requesting from happier.

Tim Lytle