irwan

MultiThreading with CURL

Nov 17th, 2011
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.95 KB | None | 0 0
  1. <?php
  2. function addHandle(&$curlHandle,$url)
  3. {
  4. $cURL = curl_init();
  5. curl_setopt($cURL, CURLOPT_URL, $url);
  6. curl_setopt($cURL, CURLOPT_HEADER, 0);
  7. curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
  8. curl_multi_add_handle($curlHandle,$cURL);
  9. return $cURL;
  10. }
  11. //execute the handle until the flag passed to function is greater then 0
  12. function ExecHandle(&$curlHandle)
  13. {
  14. $flag=null;
  15. do {
  16.     curl_multi_exec($curlHandle,$flag);//fetch pages in parallel
  17. } while ($flag > 0);
  18. }
  19.  
  20.  
  21. $list[1] = "http://www.example1.com";
  22. $list[2] = "ftp://example.com";
  23. $list[3] = "http://www.example2.com";
  24. $curlHandle = curl_multi_init();
  25. for ($i = 1;$i <= 3; $i++)
  26.  $curl[$i] = addHandle($curlHandle,$list[$i]);
  27. ExecHandle($curlHandle);
  28. for ($i = 1;$i <= 3; $i++)
  29. {
  30.  $text[$i] =  curl_multi_getcontent ($curl[$i]);
  31.  echo $text[$i];
  32. }
  33. for ($i = 1;$i <= 3; $i++)//remove the handles
  34.   curl_multi_remove_handle($curlHandle,$curl[$i]);
  35. curl_multi_close($curlHandle);
  36.  
  37. ?>
  38.  
Advertisement
Add Comment
Please, Sign In to add comment