<?php
namespace app\util;
class HttpUtils
{
/**
* 异步执行
* @
param $host
* @
param $path
* @
param array $param
* @
return false|int|string
*/
public static function requestAsync($host, $path, $param = array())
{
$query = isset($param) ? http_build_query($param) : '';
$fp = @
fsockopen($host);
if (!$fp) {
logError('连接失败');
return 'connection error';
}
stream_set_blocking($fp, 0); //非阻塞
stream_set_timeout($fp, 1);//响应超时时间( S )
$out = "POST " . $path . " HTTP/1.1\r\n";
$out .= "host:" . $host . "\r\n";
$out .= "content-length:" . strlen($query) . "\r\n";
$out .= "content-type:application/x-www-form-urlencoded\r\n";
$out .= "connection:close\r\n\r\n";
$out .= $query;
$result = @
fputs($fp, $out);
@
fclose($fp);
return $result;
}
}
<?php
namespace app\api\controller;
use app\util\HttpUtils;
use think\Controller;
use think\facade\Log;
class Test extends Controller
{
public function test(){
sleep(5);
Log::record(1111);
}
public function testAsync(){
HttpUtils::requestAsync($_SERVER['HTTP_HOST'],'/api/test/test');
dump('return');
}
}
仅供参考