0

I'm sending about 300 messages using an api using CURL requests with a for loop , It's working but takes the whole CPU.

Here is the code :

//Initializing CURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "websiteURL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("
    Content-Type: application/json",
    'Authorization: Basic '. base64_encode("XXXXX:YYYYYY")
));

//Starting for Loop.
for ($i=0; $i < 300; $i++){
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{"src": "'.$numbers_from[$i] ['number_from'].'","dst": "'.$messages[$i['international_format'].'", "text": "'.$current_message.'"}');
    $message = curl_exec($ch);
}

//Close CURL.
curl_close();

How to reduce the CPU ? , Is there is a function for that ? , Is there is an alternative for CURL?

  • Making HTTP requests is not CPU-intensive. Are you referring to how it's a blocking call? If so, you'll need to use multiple threads as PHP does not support async IO. – Dai Apr 06 '18 at 19:52
  • Is the website URL also the same server? – Mike Apr 06 '18 at 19:53
  • Alternatively, use ReactPHP ( https://reactphp.org/ ), which requires you modify your PHP environment which may not be an option for you depending on your webhost. – Dai Apr 06 '18 at 19:54
  • @Mike , No it's an api , Another website –  Apr 06 '18 at 20:13

0 Answers0