Belicoff Belicoff - 9 months ago 61
JSON Question

Using cURL in a loop - too slow probleme

I have a big database table, I fetch column to extract phone number to put it in curl POST, so how to increase time? Because I execute curl in while loop for every row found, thanks for help.

Answer Source

When using CURL you are dependent on the time it takes to communicate with the remote server. There is no way to speed a single CURL request up except making sure your server is hosted on a good service provider which is able to load the remote website quickly.

You can speed your script up by running CURL requests in parallel. To do so, move the CURL function to a separate PHP file, which receives the phone number as parameter through the command line arguments ($argv). The script should also include the code for updating the database with the CURL results.

Then, use exec() to call the script. For example, if the script is checkphone.php, call it like so:

exec('php -f /var/www/include/checkphone.php '.$phoneNumber.' > /dev/null 2>&1 &');

Note that the full directory is provided. Also, use:

/dev/null 2>&1 &

To let the system know not to wait for the script to finish running. It will execute checkphone.php then continue to iterate through the loop.

Words of caution: if you have hundreds of phone numbers, this will open hundreds of processes and would most likely crash your server. Build controls in your master script to ensure opening only a few concurrent processes at a time. This can be done by querying the database to see if the phone numbers you've sent as arguments have been updated with the additional information by the CURL script. Furthermore, make sure you don't send too many requests to the remote server too fast, otherwise you will end up DDoSing the server, which is a no-no. If you have several remote sites that can provide you with the information that you want, consider iterating between them.