The method of using curl to implement multithreading in PHP _php techniques

Source: Internet
Author: User
Tags curl php programming php regular expression

The example of this article is about the way to implement multithreading in PHP with Curl. Share to everyone for your reference, specific as follows:

Multithreading is not supported by PHP, but we can use foreach to pseudo multithreading, but this pseudo-multithreading speed is not necessarily a single path to where to go, specifically to see an example.

In the use of the foreach statement loop picture URL, and through the curl of all the pictures to save the local function, there is only one problem can be collected, now the foreach and curl combined with multiple URL request method to summarize.

Method 1: Circular Request

$SR =array (url_1,url_2,url_3);
foreach ($sr as $k => $v) {
 $curlPost = $v. ' f= incoming parameters ';
 $ch = Curl_init ($curlPost);
 curl_setopt ($ch, Curlopt_returntransfer, true); Gets the data returns
 curl_setopt ($ch, Curlopt_binarytransfer, True),//////$DATA returns the fetch data when the Curlopt_returntransfer is enabled
 = Curl_ EXEC ($ch);
 echo $k. ' # #: '. $data. ' <br> ';
}
Curl_close ($ch);

The above code needs special attention is that curl_close must be placed in the end of the Foreach loop, if placed inside, it will appear I mentioned above the multiple imgurl, can only collect a URL problem.

Method 2: Multithreading loops

<?php
multi_threads_request ($nodes) {
  $mh = Curl_multi_init ();
  $curl _array = Array ();
  foreach ($nodes as $i => $url)
  {
   $curl _array[$i] = Curl_init ($url);
   curl_setopt ($curl _array[$i], Curlopt_returntransfer, true);
   Curl_multi_add_handle ($MH, $curl _array[$i]);
  }
  $running = NULL;
  do {
   usleep (10000);
   Curl_multi_exec ($MH, $running);
  } while ($running > 0);
  $res = Array ();
  foreach ($nodes as $i => $url)
  {
   $res [$url] = curl_multi_getcontent ($curl _array[$i]);
  foreach ($nodes as $i => $url) {
   curl_multi_remove_handle ($mh, $curl _array[$i]);
  Curl_multi_close ($MH);
  return $res;
}
Print_r (Multi_threads_request (
 ' http://www.jb51.net ',
 ' http://tools.jb51.net ',
));

Here the main use of curl_multi_init () to implement multiple URL requests, but because PHP itself does not support multithreading, so the pseudo-multithreaded speed is not necessarily faster than the single thread.

More about PHP Interested readers can view the site topics: "Php Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.