1 curl :需要php支持php_curl
<?php
$url = "http://www.sina.com.cn/";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$info = curl_exec($ch) or die("curl失败<br/>");
echo strlen($info)."<br/>";
2 file_get_contents:需要支持all_url_fopen
<?php
$url = "http://www.sina.com.cn/";
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 60
)
)
);
$info=file_get_contents($url, 0, $ctx)or die("file_get_contents超时<br/>");
echo strlen($info)."<br/>";
3 fsockopen
<?php
$url = "http://www.sina.com.cn/";
$urlInfo = parse_url($url);
$host = $urlInfo['host'];
$sock = fsockopen($host, 80, $errno, $errstr, 60);
if (!$sock) {
echo "fsockopen$errno<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: " . $host . "\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($sock, $out);
while (!feof($sock)) {
$info = $info . fgets($sock, 128);
}
fclose($sock);
echo strlen($info)."<br/>";
}
目前测试:
运行时间(s) | |
ab -n1000 -c1000 | |
File_get_contents | 1.403 |
curl | 1.390 |
fsockopen | 1.270 |
ab -n2000 -c1000 | |
File_get_contents | 4.446 |
curl | 4.786 |
fsockopen | 5.185 |
ab -n3000 -c1000 | |
File_get_contents | 4.177 |
curl | 4.467 |
fsockopen | 4.649 |
ab -n4000 -c1000 | |
File_get_contents | 5.656 |
curl | 5.153 |
fsockopen | 5.324 |
ab -n5000 -c1000 | |
File_get_contents | 14.111 |
curl | 11.737 |
fsockopen | 13.123 |