用PHP写一个多并发任务同时进行访问网站httphostgay 且强行执行5分钟
以下是一个使用PHP进行多并发任务同时访问网站并强行执行5分钟的示例代码:
<?php
// 设置脚本执行时间为5分钟
set_time_limit(300);
// 并发任务数
$taskCount = 10;
// 创建多个并发任务
$curlHandles = [];
for ($i = 0; $i < $taskCount; $i++) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://host.gay');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
// 将 curl 句柄添加到数组中
$curlHandles[$i] = $ch;
}
// 创建批处理句柄
$mh = curl_multi_init();
// 将多个 curl 句柄添加到批处理句柄中
foreach ($curlHandles as $ch) {
curl_multi_add_handle($mh, $ch);
}
$active = null;
// 执行并发任务直到超时
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc === CURLM_CALL_MULTI_PERFORM);
// 循环处理批处理句柄中的任务
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) === -1) {
usleep(100);
}
// 继续处理任务
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc === CURLM_CALL_MULTI_PERFORM);
}
// 关闭所有 curl 句柄
foreach ($curlHandles as $ch) {
curl_multi_remove_handle($mh, $ch);
curl_close($ch);
}
// 关闭批处理句柄
curl_multi_close($mh);
上述代码会创建10个并发任务同时访问网站 http://host.gay。脚本会强制执行5分钟(300秒),然后结束并发任务。请注意,根据您的服务器配置和网络速度,同时进行的并发任务数可能会有所不同
原文地址: https://www.cveoy.top/t/topic/h3Vh 著作权归作者所有。请勿转载和采集!