在PHP中实现Parallel cURL的最简单方法是什么?

时间:2018-06-19 13:08:21

标签: php curl

我有一个带有以下代码的.php文件。我想并行发出curl请求,因为在下面的代码中-每个请求至少花费1秒。我希望在这段时间内至少处理10个请求。我该如何实现?

<?php
$data = file("test.txt", FILE_IGNORE_NEW_LINES);
$count = count($data);
for( $i = 0; $i<$count; $i++)
{
    $mob=$data[$i];
    $url = "http://127.0.0.1/test.php?category=243&to=$mob";
    echo $url."\n";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $htmlText = curl_exec($ch);
    curl_close($ch);
}
?>

test.txt文件包含如下数据:

100100
200200
300300
......
......

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可能要使用:curl_multi_add_handle

尽管您也可以使用exec,但这会更加困难