1

In my application I need to do 1000 and sometimes even more CURL requests to the same URL. So each time i do a foreach loop and send the data one by one:

foreach ($data as $id => $value) {

    $queryUpdate = [
        'id' => $id,
        'data' => [
            'ref_id' => $value['id'],
            'name' => $value['title'],
            'description' => $value['desc'],
        ]
    ];

    curl_setopt_array($ch, array(
        CURLOPT_URL => 'https://test/blabla/bla',
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => http_build_query(
            $queryUpdate
        ),
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array(
            "Accept: application/json",
            "Content-type: application/x-www-form-urlencoded"
        ),
    ));
    $response = curl_exec($ch);
    $rsp = json_decode($response, true);
    if ($rsp['response']['status'] != 1) {
        var_dump($rsp);
        die;
    }

    echo  "$i) Updated at" .  date("h:i:s") . "<br />";

    $i++;
}
curl_close($ch);

So it takes around 3-4 mins to do 200 requests. After 5mins I get timeout from curl(because i exceed 300seconds).

Now I can probably increase the timeout lenght I didnt look, but I need a different solution. A solution that would speed up all these curl proccesses. Is there a way?

Dominykas55
  • 1,231
  • 14
  • 45
  • 1
    This isn't something you're going to solve very easily in a single PHP script. Look into a message queuing solution like RabbitMQ to split the task between multiple "worker" programs. – Dave Ross Apr 22 '16 at 10:17
  • If you don't want to use [PHP multi-threading](http://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications), the only way to increase performance is use [multi-curl](http://www.php.net/manual/en/function.curl-multi-init.php) feature. It's a bit hard to understand how to use it properly. So it's better to use a multi-curl wrapper such as [Rolling Curl Mini](https://github.com/hindmost/rolling-curl-mini) (plug!). – hindmost Apr 22 '16 at 12:39

0 Answers0