Home  >  Article  >  Backend Development  >  Implementation code for php curl to open URLs in batches (curl_multi class)

Implementation code for php curl to open URLs in batches (curl_multi class)

WBOY
WBOYOriginal
2016-07-25 08:55:051863browse
  1. /*
  2. * After testing, curl_multi is faster than the Foreach loop when there are more than four URLs..
  3. * by wc1217
  4. * edit: bbs.it-home.org
  5. */
  6. class curl_multi{
  7. //Curl handle
  8. //private $curl_handle = null;
  9. //Web address
  10. private $url_list = array();
  11. //Parameters
  12. private $curl_setopt = array(
  13. 'CURLOPT_RETURNTRANSFER' => 1, / /The result is returned to the variable
  14. 'CURLOPT_HEADER' => 0, //Do you want HTTP header?
  15. 'CURLOPT_NOBODY' => 0, //No content?
  16. 'CURLOPT_FOLLOWLOCATION' => 0, //Automatic tracking
  17. ' CURLOPT_TIMEOUT' => 6//Timeout(s)
  18. );
  19. function __construct($seconds = 30){
  20. set_time_limit($seconds);
  21. }
  22. /*
  23. * Set URL
  24. * @list array
  25. * /
  26. public function setUrlList($list = array()){
  27. $this->url_list = $list;
  28. }
  29. /*
  30. * Set parameters
  31. * @cutPot array
  32. */
  33. public function setOpt($cutPot ){
  34. $this->curl_setopt = $cutPot + $this->curl_setopt;
  35. }
  36. /*
  37. * Execute
  38. * @return array
  39. */
  40. public function exec(){
  41. $mh = curl_multi_init( );
  42. foreach($this->url_list as $i => $url){
  43. $conn[$i] = curl_init($url);
  44. foreach($this->curl_setopt as $key = > $val){
  45. curl_setopt($conn[$i], preg_replace('/(CURLOPT_w{1,})/ie', '$0', $key), $val);
  46. }
  47. curl_multi_add_handle($mh , $conn[$i]);
  48. }
  49. $active = false;
  50. do{
  51. $mrc ​​= curl_multi_exec($mh, $active);
  52. }while($mrc == CURLM_CALL_MULTI_PERFORM);
  53. while( $active and $mrc ​​== CURLM_OK){
  54. if(curl_multi_select($mh) != -1){
  55. do{
  56. $mrc ​​= curl_multi_exec($mh, $active);
  57. }while($mrc == CURLM_CALL_MULTI_PERFORM) ;
  58. }
  59. }
  60. $res = array();
  61. foreach($this->url_list as $i => $url){
  62. $res[$i] = curl_multi_getcontent($conn[$i]) ;
  63. curl_close($conn[$i]);
  64. curl_multi_remove_handle($mh, $conn[$i]); //Release resources immediately after use
  65. }
  66. curl_multi_close($mh);
  67. return $res;
  68. }
  69. }
  70. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn