Example of php using curl to simulate multithreading to realize batch processing function

  • 2021-12-13 16:26:44
  • OfStack

This article describes the example of php using curl simulation of multithreading to achieve batch processing function. Share it for your reference, as follows:

php simulation multithreading uses curl library, which is very powerful and can do many things, such as simulation login, file upload/download, data collection and so on.

The following is my code, which is very simple, and some functions will not be used yet.


<?php
$node_urls=array('http://www.baidu.com','http://www.google.com.hk');
$ch=array();
$mh=curl_multi_init();
$ch[0]=curl_init($node_urls[0]);
$ch[1]=curl_init($node_urls[1]);
for($i=0;$i<2;$i++)
{
curl_setopt($ch[$i],CURLOPT_RETURNTRANSFER,1);
curl_multi_add_handle($mh,$ch[$i]);
}
$running=NULL;
do{
  usleep(10000);
  curl_multi_exec($mh,$running);
}while($running>0);
$res=array();
for($j=0;$j<2;$j++)
{
  $res[$j]=curl_multi_getcontent($ch[$j]);
}
for($k=0;$k<2;$k++)
{
  curl_multi_remove_handle($mh,$ch[$k]);
}
curl_multi_close($mh);
print_r($res);
?>

This code is the code of crawling Baidu and Google in parallel, and then slowly improve it later.

There are still many things to do.

For more readers interested in PHP related contents, please check the topics of this site: "Summary of php curl Usage", "Summary of PHP Network Programming Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Tutorial on PHP Data Structure and Algorithm" and "Summary of json Format Data Operation Skills in PHP"

I hope this article is helpful to everyone's PHP programming.


Related articles: