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.
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().
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
// Print the curl info like http response code, content type etc.
// close the curl handle to free system resources
Figure 2: Initialization and execution of the cURL sessions in PHP
As you can see from the Figure 2 on the previous page, the curl_getinfo($curl) method lists some curl info, like the url, connect_time and so on:
The example below simulates the multithreading mechanism by initializating a new session and returns a cURL handle, sets the options for the http://www.google.ro and http://ro.yahoo.com/?p=us URLs, executes the cURL session and then prints the results using the curl_getinfo method:
// Creating an array of URLs that will be used below
$urls_array = array('http://www.google.ro',' http://ro.yahoo.com/?p=us');
$curl_array = array();
// Initializes a new session and return a cURL multi handle for use with the curl_setopt(), curl_exec() and curl_close() functions
$curl_multi_handle = curl_multi_init();
// Use the urls_array array for initialize a single session, set the transfer and to add a normal cURL handle to a cURL multi handle
for($i = 0; $i < count($urls_array); $i++)
//Initialize a single cURL session
$curl_array[$i] = curl_init($url);
//Setting the option for a cURL transfer
curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true);
//Add a normal cURL handle to a cURL multi handle
//Run the sub-connections of the current cURL handle
} while($running > 0) ;
echo "results: ";
for($i = 0; $i < count($urls_array); $i++)
//Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
$results = curl_multi_getcontent ( $curl_array[$i] );
Figure 3: The above application simulates the cURL mechanism using some control structures and some of the most important cURL methods like curl_setopt, curl_multi_exec and curl_multi_getcontent.
In this article you have seen how to make the libcurl library functional in PHP, how to executes cURL sessions, how to print curl info like HTTP response code and content type, and how to simulates the cURL mechanism.
About the Author
Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies: XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests. to e-mail her.