使用时如何找出每个请求在什么时候收到响应multi curl?假设我使用phpvia发送了 10 个异步请求,multi curl并且我想知道确切的时间 - 请求是在什么时候发送的,而这 10 个请求中的每一个接收到的响应都精确到毫秒?
UPD
让我更详细地解释一下情况。multi curl我通过这样的方式向服务器发送 2 个请求:
function multirequest($bodys, $headers)
{
$multi = curl_multi_init();
$handles = [];
$htmls = [];
for($i=0; $i<count($bodys);$i++)
{
$ch = curl_init('https://...');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodys[$i]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers[$i]);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'get_time');
curl_multi_add_handle($multi, $ch);
array_push($handles, $ch);
}
do {
$mrc = curl_multi_exec($multi, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK)
{
if (curl_multi_select($multi) == -1)
{
usleep(1);
}
do
{
$mrc = curl_multi_exec($multi, $active);
}while($mrc == CURLM_CALL_MULTI_PERFORM);
}
foreach($handles as $channel)
{
$html = curl_multi_getcontent($channel);
$htmls[] = $html;
curl_multi_remove_handle($multi, $channel);
}
curl_multi_close($multi);
return $htmls;
}
该函数get_time打印响应时间和内容:
function get_time($ch, $string)
{
echo "<br>".microtime(false)."<br>".$string."<br>";
}
为了响应这两个请求,服务器以纳秒的精度返回它的时间(测试函数),最重要的是,这个时间从十分之几秒到几秒不等,因为请求multi curl不是异步发送的(无需等待响应)但顺序 - 即 首先收到对 1 个请求的响应,然后只发送了第二个。响应时间和接收数据的差异太大,我试图了解为什么会发生这种情况。为此,您需要准确了解microtime()每个multi curl请求的发送时间。
您可以使用此功能打印当前时间:(
echo system('date +%s%N');仅适用于 Linux),否则使用该功能microtime(文档)。如果你想知道每个操作需要多长时间:你可以microtime在开始查询之前将值写入一个变量,在callback查询中你可以计算当前时间(毫秒)与过去时间变量之间的差异。