{"id":1315,"date":"2018-01-19T19:22:41","date_gmt":"2018-01-19T19:22:41","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/php-multithreading-with-curl\/"},"modified":"2018-01-19T19:24:41","modified_gmt":"2018-01-19T19:24:41","slug":"php-multithreading-with-curl","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/php-multithreading-with-curl\/","title":{"rendered":"PHP Multithreading with cURL"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Octavia Andreea Anghel<\/div>\n<div class=\"\">on June 14, 2011<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<div class=\"articlePara\">\nSome 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.\n<\/div>\n<div class=\"articlePara\">\nThis 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.\n<\/div>\n<div class=\"articlePara\">\nPHP 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&#8217;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 &#8220;;&#8221; 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.\n<\/div>\n<p><img decoding=\"async\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/Figure1.jpg\"\/><\/p>\n<div class=\"articlePara\">\n<strong>Figure 1:<\/strong> Checking if the cURL was correctly installed using the <code>phpinfo()<\/code> method.\n<\/div>\n<div class=\"articlePara\">\nAfter executing the <code>phpinfo()<\/code> 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.\n<\/div>\n<div class=\"articlePara\">\nSome 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.\n<\/div>\n<pre>\nCURLOPT_URL\nCURLOPT_FILE\nCURLOPT_HEADER\nCURLOPT_RETURNTRANSFER\nCURLOPT_BINARYTRANSFER\nCURLOPT_TIMEOUT\n<\/pre>\n<div class=\"articlePara\">\nAll the predefined constant descriptions and uses are described in the <code>curl_setopt()<\/code> and <code>curl_getinfo()<\/code> documentation.<\/p>\n<ol>\n<li>bool curl_setopt (resource $ch , int $option , mixed $value): Sets an option for a cURL transfer\n<\/li>\n<li>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 <code>curl_setopt()<\/code>.\n<\/li>\n<\/ol>\n<\/div>\n<div class=\"articlePara\">\nYou can review <a href=\"http:\/\/www.php.net\/manual\/en\/ref.curl.php\" target=\"_new\">all the cURL functions<\/a> and if you are new to cURL, you can also <a href=\"http:\/\/www.php.net\/manual\/en\/curl.examples.php\" target=\"_new\">view some examples<\/a>.\n<\/div>\n<div class=\"articlePara\">\nThe code below initializes a new session and returns a cURL handle, sets the options for the <em>http:\/\/www.yahoo.com<\/em> URL, executes the cURL session, and then prints the results using the <code>curl_getinfo<\/code> method:\n<\/div>\n<pre>\n\/\/Initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec() and curl_close() functions\n$curl = curl_init();\n\n\/\/Sets an option on the given cURL session handle like url, timeout, return transfer\ncurl_setopt($curl, CURLOPT_URL, 'http:\/\/www.google.ro);\ncurl_setopt($curl, CURLOPT_TIMEOUT, 30);\ncurl_setopt($curl, CURLOPT_RETURNTRANSFER, true);\n\n\/\/ Execute the given cURL session (get the content of the url and put it into the output variable)\n$output = curl_exec($curl);\n\n\/\/ Outputs the result\necho $output;\n\n\/\/ Print the curl info like http response code, content type etc.\necho '\n';\n print_r (curl_getinfo($curl));\n echo '\n';\n\n\/\/ close the curl handle to free system resources\ncurl_close($curl);\n\n?&gt;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/Figure2.jpg\" width=\"600\" height=\"500\"\/><\/p>\n<div class=\"articlePara\">\n<strong>Figure 2:<\/strong> Initialization and execution of the cURL sessions in PHP\n<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"Octavia_Anghel060720114658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"Octavia_Anghel060720114658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>When there are many tasks to execute in a given script, it may take a long time to finish all of them if they are executed sequentially, i.e., one<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1315","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1315"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1315\/revisions"}],"predecessor-version":[{"id":2159,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1315\/revisions\/2159"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}