#native_company# #native_desc#

PHP Multithreading with cURL Page 2

By Octavia Andreea Anghel
on 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] => http://www.google.ro
[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 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 0) ; echo "results: "; for($i = 0; $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.