我正在使用 cURL 向 3rd 方 API 发送发布请求,但我担心他们服务器上的问题可能会真正减慢我的进程,因此,我试图通过更改来模拟这样的问题url 是随机的,但即使我将超时设置为 1 秒,脚本也需要大约 11 秒才能完成
...
$start_time = microtime(true);
$url = 'https://awebsitethatdefinitelydoesnotexist.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$end_time = microtime(true);
$execution_time = ($end_time - $start_time);
$output = array(
"Response" => $response,
"HTTP Code" => $code,
"Exec time" => $execution_time
);
return $output;
输出:
Exec time: 11.100919008255005
HTTP Code: 0
Response: false
编辑:我想我应该添加更多信息,我使用 Laravel 作为我的框架,并且我正在使用来自浏览器的 ajax 请求调用该函数:
$.ajax({
url: '/myroute',
type: 'POST',
data: {
...
}
})