Home  >  Article  >  Backend Development  >  PHP CURL multi-threaded GET/POST class

PHP CURL multi-threaded GET/POST class

WBOY
WBOYOriginal
2016-07-25 08:44:061270browse
  1. /****************************************************************
  2. PHP CURL 多线程 GET/POST
  3. curl(array('url?get=data','url'),array('','post_data'));
  4. *****************************************************************/
  5. function curl($urls,$post) {
  6. $queue = curl_multi_init();
  7. $map = array();
  8. foreach ($urls as $key => $url) {
  9. $ch = curl_init();
  10. curl_setopt($ch, CURLOPT_URL, $url);
  11. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  13. curl_setopt($ch, CURLOPT_POSTFIELDS, $post[$key]);
  14. curl_setopt($ch, CURLOPT_HEADER, 0);
  15. curl_setopt($ch, CURLOPT_NOSIGNAL, true);
  16. curl_multi_add_handle($queue, $ch);
  17. $map[(string) $ch] = $url;
  18. }
  19. $responses = array();
  20. do {
  21. while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) ;
  22. if ($code != CURLM_OK) { break; }
  23. while ($done = curl_multi_info_read($queue)) {
  24. $error = curl_error($done['handle']);
  25. $results = curl_multi_getcontent($done['handle']);
  26. $responses[$map[(string) $done['handle']]] = compact('error', 'results');
  27. curl_multi_remove_handle($queue, $done['handle']);
  28. curl_close($done['handle']);
  29. }
  30. if ($active > 0) {
  31. curl_multi_select($queue, 0.5);
  32. }
  33. } while ($active);
  34. curl_multi_close($queue);
  35. return $responses;
  36. }
复制代码

多线程, PHP, CURL


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