Advertisement
Guest User

Untitled

a guest
Sep 27th, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1.  
  2.  
  3.  
  4. <?php
  5.  
  6. function multiRequest($data, $options = array()) {
  7.  
  8.   // array of curl handles
  9.   $curly = array();
  10.   // data to be returned
  11.   $result = array();
  12.  
  13.   // multi handle
  14.   $mh = curl_multi_init();
  15.  
  16.   // loop through $data and create curl handles
  17.   // then add them to the multi-handle
  18.   foreach ($data as $id => $d) {
  19.  
  20.     $curly[$id] = curl_init();
  21.  
  22.     $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
  23.     curl_setopt($curly[$id], CURLOPT_URL,            $url);
  24.     curl_setopt($curly[$id], CURLOPT_HEADER,         0);
  25.     curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
  26.    
  27.  
  28.     // post?
  29.     if (is_array($d)) {
  30.       if (!empty($d['post'])) {
  31.         curl_setopt($curly[$id], CURLOPT_POST,       1);
  32.         curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
  33.       }
  34.     }
  35.  
  36.     // extra options?
  37.     if (!empty($options)) {
  38.       curl_setopt_array($curly[$id], $options);
  39.     }
  40.  
  41.     curl_multi_add_handle($mh, $curly[$id]);
  42.   }
  43.  
  44.  curl_multi_setopt($mh, CURLMOPT_MAXCONNECTS, 2);
  45.   // execute the handles
  46.   $running = null;
  47.   do {
  48.     curl_multi_exec($mh, $running);
  49.   } while($running > 0);
  50.  
  51.  
  52.   // get content and remove handles
  53.   foreach($curly as $id => $c) {
  54.     $result[$id] = curl_multi_getcontent($c);
  55.     curl_multi_remove_handle($mh, $c);
  56.   }
  57.  
  58.   // all done
  59.   curl_multi_close($mh);
  60.  
  61.   return $result;
  62. }
  63.  
  64.  
  65. $data = array(
  66.     'http://www.codechef.com/status/CLETAB,tacoder',
  67.     'http://www.codechef.com/status/CRAWA,tacoder',
  68.     'http://www.codechef.com/status/EQUAKE,tacoder',
  69.     'http://www.codechef.com/status/MOU2H,tacoder',
  70.     'http://www.codechef.com/status/PRGIFT,tacoder',
  71.     'http://www.codechef.com/status/PUSHFLOW,tacoder',
  72.     'http://www.codechef.com/status/REVERSE,tacoder',
  73.     'http://www.codechef.com/status/SEASHUF,tacoder',
  74.     'http://www.codechef.com/status/SIGFIB,tacoder',
  75.     'http://www.codechef.com/status/TSHIRTS,tacoder'
  76. );
  77. $r = multiRequest($data);
  78.  
  79. print_r($r);
  80.  
  81. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement