<?php
$start = microtime(true);
$fp=fsockopen('localhost',80,$errno,$errstr,5);
if(!$fp){
echo "$errstr ($errno)<br />\n";
}
$head = "GET /get.php HTTP/1.1 \r\n";
$head .= "Host: localhost \r\n";
$head .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0 \r\n";
$head .= "Connection: Close \r\n\r\n";
//发送请求
fputs($fp,$head);
/* 忽略结果
while(!feof($fp)) {
echo fgets($fp,128);
}
*/
fclose($fp);
header("Content-Type: text/html;charset=utf-8");
$end = microtime(true);
echo "脚本执行时间".($end - $start).'<br>';
?>
异步执行的代码:
<?php
ignore_user_abort(1); //由于异步请求后会立即断开连接,默认会终止脚本,忽略掉
set_time_limit(0); //取消脚本执行延时上限
for ($i = 1; $i < 10; $i++)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.haiyun.me/?$i");
curl_setopt($ch, CURLOPT_REFERER, "http://www.test.com");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0");
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,3);
curl_setopt($ch,CURLOPT_TIMEOUT,3);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_exec($ch);
}
?>