PHPBuilder - PHP Multithreading with cURL Page 2

RSS Twitter

PHP Multithreading with cURL - Page 2

by: Octavia Andreea Anghel
June 14, 2011

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:
[url] =>
[content_type] => text/html; charset=ISO-8859-2
[http_code] => 200
[header_size] => 604
[request_size] => 52
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.125
[namelookup_time] => 0.265
[connect_time] => 0.5
[pretransfer_time] => 0.5
[size_upload] => 0
[size_download] => 27552
[speed_download] => 24490
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => 0
[starttransfer_time] => 0.734
[redirect_time] => 0
[certinfo] => Array

The example below simulates the multithreading mechanism by initializating a new session and returns a cURL handle, sets the options for the and 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('','');

$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++) { $url =$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 curl_multi_add_handle($curl_multi_handle, $curl_array[$i]); } do { //Run the sub-connections of the current cURL handle curl_multi_exec($curl_multi_handle,$running); } 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] ); echo $results; } ?>
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.

« Previous Page

Comment and Contribute

Your comment has been submitted and is pending approval.

Octavia Andreea Anghel



(Maximum characters: 1200). You have characters left.