PHP simulation multi-threaded request code example

WBOY
Release: 2016-07-25 08:44:21
Original
855 people have browsed it
下面是PHP模拟多线程请求代码示例
  1. multi_threads_request($nodes){
  2. $mh = curl_multi_init();
  3. $curl_array = array();
  4. foreach($nodes as $i => $url)
  5. {
  6. $curl_array[$i] = curl_init($url);
  7. curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true);
  8. curl_multi_add_handle($mh, $curl_array[$i]);
  9. }
  10. $running = NULL;
  11. do {
  12. usleep(10000);
  13. curl_multi_exec($mh,$running);
  14. } while($running > 0);
  15. $res = array();
  16. foreach($nodes as $i => $url)
  17. {
  18. $res[$url] = curl_multi_getcontent($curl_array[$i]);
  19. }
  20. foreach($nodes as $i => $url){
  21. curl_multi_remove_handle($mh, $curl_array[$i]);
  22. }
  23. curl_multi_close($mh);
  24. return $res;
  25. }
  26. print_r(multi_threads_request(array(
  27. 'http://www.open-open.com',
  28. 'http://www.baidu.com',
  29. ));
  30. ?>
复制代码

多线程, PHP


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template