Some applications need to perform several tasks that may take a while to finish. When there are many tasks to execute, it may take a long time to finish all of them if they are executed sequentially, i.e., one after another. A possible solution for this problem is to execute several tasks at the same time using separate processes or threads. PHP has the pcntl extension that can be used to run multiple processes in parallel. However, this extension is only available in Linux or UNIX-like operating systems.
This article explains an alternative solution that consists of sending multiple HTTP requests to the same Web server on which PHP is running. Each HTTP request triggers the execution of a different task. Many requests can be run at the same time without having to wait for each one to finish. This solution can run in Windows and all other operating systems supported by PHP, including PHP environments on which the pcntl extension is not installed. As you may know, PHP has no native support for multithreading like Java, but using the cURL (Client URL) extension makes multithreading possible in PHP.
PHP supports the libcurl library that allows you to connect and communicate to many different types of servers with many different types of protocols. Libcurl currently supports the http, https, ftp, gopher, telnet, dict, file and ldap protocols. Libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form-based upload, proxies, cookies and user+password authentication. These library functions have been added to PHP 4.0.2. To use this library in PHP, remove the “;” from extension=php_curl.dll in php.ini, put ssleay32.dll and libeay32.dll in Windows/System32, and copy the php_curl.dll into Windows/System32.
Figure 1: Checking if the cURL was correctly installed using the
phpinfo()
method.
After executing the
phpinfo()
method, you should get something like the above figure. As you may notice, in this article we will use cURL 7.21.0 and the dict, file, ftp, ftps, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet and tftp protocols.
Some of the most used predefined constants are listed below, and they will be used during this article. The constants defined by the liburl extension are available only when this extension is compiled into PHP or dynamically loaded at runtime.
CURLOPT_URL CURLOPT_FILE CURLOPT_HEADER CURLOPT_RETURNTRANSFER CURLOPT_BINARYTRANSFER CURLOPT_TIMEOUT
All the predefined constant descriptions and uses are described in the
curl_setopt()
and curl_getinfo()
documentation.
- bool curl_setopt (resource $ch , int $option , mixed $value): Sets an option for a cURL transfer
- bool curl_setopt_array (resource $ch , array $options): Sets multiple options for a cURL session. This function is useful for setting a large amount of cURL options without repetitively calling
curl_setopt()
.
You can review all the cURL functions and if you are new to cURL, you can also view some examples.
The code below initializes a new session and returns a cURL handle, sets the options for the http://www.yahoo.com URL, executes the cURL session, and then prints the results using the
curl_getinfo
method:
//Initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec() and curl_close() functions $curl = curl_init(); //Sets an option on the given cURL session handle like url, timeout, return transfer curl_setopt($curl, CURLOPT_URL, 'http://www.google.ro); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Execute the given cURL session (get the content of the url and put it into the output variable) $output = curl_exec($curl); // Outputs the result echo $output; // Print the curl info like http response code, content type etc. echo ' '; print_r (curl_getinfo($curl)); echo ' '; // close the curl handle to free system resources curl_close($curl); ?>
Figure 2: Initialization and execution of the cURL sessions in PHP