test.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. *
  4. * curl_multi_*简单运用
  5. *
  6. * @author: rudy
  7. * @date: 2016/07/12
  8. */
  9. /**
  10. * 根据url,postData获取curl请求对象,这个比较简单,可以看官方文档
  11. */
  12. function getCurlObject($url, $postData = array(), $header = array())
  13. {
  14. $options = array();
  15. $url = trim($url);
  16. $options[CURLOPT_URL] = $url;
  17. $options[CURLOPT_TIMEOUT] = 10;
  18. $options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36';
  19. $options[CURLOPT_RETURNTRANSFER] = true;
  20. // $options[CURLOPT_PROXY] = '127.0.0.1:8888';
  21. foreach ($header as $key => $value) {
  22. $options[$key] = $value;
  23. }
  24. if (!empty($postData) && is_array($postData)) {
  25. $options[CURLOPT_POST] = true;
  26. $options[CURLOPT_POSTFIELDS] = http_build_query($postData);
  27. }
  28. if (stripos($url, 'https') === 0) {
  29. $options[CURLOPT_SSL_VERIFYPEER] = false;
  30. }
  31. $ch = curl_init();
  32. curl_setopt_array($ch, $options);
  33. return $ch;
  34. }
  35. // 创建三个待请求的url对象
  36. $chList = array();
  37. $chList[] = getCurlObject('https://www.baidu.com');
  38. $chList[] = getCurlObject('http://www.jd.com');
  39. $chList[] = getCurlObject('http://www.jianshu.com/');
  40. // 创建多请求执行对象
  41. $downloader = curl_multi_init();
  42. // 将三个待请求对象放入下载器中
  43. foreach ($chList as $ch) {
  44. curl_multi_add_handle($downloader, $ch);
  45. }
  46. // 轮询
  47. do {
  48. while (($execrun = curl_multi_exec($downloader, $running)) == CURLM_CALL_MULTI_PERFORM);
  49. if ($execrun != CURLM_OK) {
  50. break;
  51. }
  52. // 一旦有一个请求完成,找出来,处理,因为curl底层是select,所以最大受限于1024
  53. while ($done = curl_multi_info_read($downloader)) {
  54. // 从请求中获取信息、内容、错误
  55. $info = curl_getinfo($done['handle']);
  56. $output = curl_multi_getcontent($done['handle']);
  57. $error = curl_error($done['handle']);
  58. // 将请求结果保存,我这里是打印出来
  59. print $output;
  60. // print "一个请求下载完成!\n";
  61. // 把请求已经完成了得 curl handle 删除
  62. curl_multi_remove_handle($downloader, $done['handle']);
  63. }
  64. // 当没有数据的时候进行堵塞,把 CPU 使用权交出来,避免上面 do 死循环空跑数据导致 CPU 100%
  65. if ($running) {
  66. $rel = curl_multi_select($downloader, 1);
  67. if ($rel == -1) {
  68. usleep(1000);
  69. }
  70. }
  71. if ($running == false) {
  72. break;
  73. }
  74. } while (true);