多数情况下,可能需要进行异步请求,fsockopen是一个不错的选择
下面参数进行详解,看代码吧:
<?php
set_time_limit(300);
function thread($count=1) {
for($i=0;$i<$count;$i++){
$fp=fsockopen($_SERVER['HTTP_HOST'],80); // $timeout // 创建连接时的超时时间
// stream_set_blocking($fp, 0); // 阻塞模式 0 非阻塞, 1 阻塞
stream_set_timeout($fp, 2); // 读取流时的超时时间
fwrite($fp,"GET http://localhost/testphp/thread/thread2.php\r\n\r\n");
$r = fgets($fp,1024); // 如果stream_set_blocking没有设置 读取$fp 阻塞, 不读取$fp 非阻塞 (ps:前提是stream_set_blocking没有设置,设置了就按stream_set_blocking所设置的值)
$m = stream_get_meta_data($fp); // 返回现有 stream 的信息 $fp
var_dump($m);
fclose($fp);
soc_log('nosocket', date("Y-m-d H:i:s", time()));
}
}
function soc_log($script,$start_time)
{
$fp = fopen($script.".log", 'a+');
fputs($fp, 'start time is ' . date("Y-m-d H:i:s", time()) . "\n");
fclose($fp);
}
thread(2);
// for($j=0;$j<5;$j++){
// soc_log('nosocket', date("Y-m-d H:i:s", time()));
// }
?>