此代碼從$ url獲取標題和內容,並將其打印到瀏覽器。它真的很慢,並不是因爲服務器。我該如何改進?改進HTTP GET PHP腳本
$headers = get_headers($url);
foreach ($headers as $value)
header($value);
$fh = fopen($url, "r");
fpassthru($fh);
感謝
此代碼從$ url獲取標題和內容,並將其打印到瀏覽器。它真的很慢,並不是因爲服務器。我該如何改進?改進HTTP GET PHP腳本
$headers = get_headers($url);
foreach ($headers as $value)
header($value);
$fh = fopen($url, "r");
fpassthru($fh);
感謝
爲什麼要做兩個請求?
$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;
我不知道爲什麼你會在第6行打開一個連接那裏,如果你已經擁有並且已經打印出來了頭。這不僅僅是打印出頭文件嗎?
如果你真的希望只代理一個頁面,捲曲功能更有效:
<?
$curl = curl_init("http://www.google.com");
curl_setopt($curl, CURLOPT_HEADER, true);
curl_exec($curl);
curl_close($curl);
?>
當然,捲曲,必須啓用在你的服務器上,但這並不罕見。
想要提出一個代理嗎?如果是這樣,這裏是一個配方,在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]
您可以通過改變$ url到知道網站迅速查明緩慢,或甚至是本地的網絡服務器。似乎可能的唯一情況是服務器的響應緩慢。
當然由GZipp的建議,如果你要輸出的文件內容,以及,只是一個單一的請求去做。這會讓你請求的服務器更快樂。
$ url的值是多少?看起來你正在打開第6行(fopen)的遠程站點,這可能是他緩慢的原因。 – Langdon 2009-11-12 15:03:57
驗證每個輸入。即使允許,也不要忽略每個塊的{和}。 – erenon 2009-11-12 15:06:45
您可以使用分析器來檢查瓶頸。看看xDebug。 – erenon 2009-11-12 15:07:24