Maison  >  Article  >  développement back-end  >  使用 PHP 和 cURL 获取 URLs

使用 PHP 和 cURL 获取 URLs

WBOY
WBOYoriginal
2016-07-25 09:08:381075parcourir
http://css.dzone.com/articles/retrieving-urls-parallel-curl
  1. class Footo_Content_Retrieve_HTTP_CURLParallel
  2. {
  3. /**
  4. * Fetch a collection of URLs in parallell using cURL. The results are
  5. * returned as an associative array, with the URLs as the key and the
  6. * content of the URLs as the value.
  7. *
  8. * @param array $addresses An array of URLs to fetch.
  9. * @return array The content of each URL that we've been asked to fetch.
  10. **/
  11. public function retrieve($addresses)
  12. {
  13. $multiHandle = curl_multi_init();
  14. $handles = array();
  15. $results = array();
  16. foreach($addresses as $url)
  17. {
  18. $handle = curl_init($url);
  19. $handles[$url] = $handle;
  20. curl_setopt_array($handle, array(
  21. CURLOPT_HEADER => false,
  22. CURLOPT_RETURNTRANSFER => true,
  23. ));
  24. curl_multi_add_handle($multiHandle, $handle);
  25. }
  26. //execute the handles
  27. $result = CURLM_CALL_MULTI_PERFORM;
  28. $running = false;
  29. // set up and make any requests..
  30. while ($result == CURLM_CALL_MULTI_PERFORM)
  31. {
  32. $result = curl_multi_exec($multiHandle, $running);
  33. }
  34. // wait until data arrives on all sockets
  35. while($running && ($result == CURLM_OK))
  36. {
  37. if (curl_multi_select($multiHandle) > -1)
  38. {
  39. $result = CURLM_CALL_MULTI_PERFORM;
  40. // while we need to process sockets
  41. while ($result == CURLM_CALL_MULTI_PERFORM)
  42. {
  43. $result = curl_multi_exec($multiHandle, $running);
  44. }
  45. }
  46. }
  47. // clean up
  48. foreach($handles as $url => $handle)
  49. {
  50. $results[$url] = curl_multi_getcontent($handle);
  51. curl_multi_remove_handle($multiHandle, $handle);
  52. curl_close($handle);
  53. }
  54. curl_multi_close($multiHandle);
  55. return $results;
  56. }
  57. }
复制代码


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:比较两个字符串的相似度 Article suivant:curl远程获取验证码