/**
 * http://techblog.yahoo.co.jp/architecture/api1_curl_multi/
 * 複数URLのコンテンツ、及び通信ステータスを一括取得する。
 * サンプル:
 *   $urls = array( "http://〜", "http://〜", "http://〜" );
 *   $results = getMultiContents($urls);
 *   print_r($results);
 */
function getMultiContents($url_list)
{
    // マルチハンドルの用意
    $mh = curl_multi_init();
    // URLをキーとして、複数のCurlハンドルを入れて保持する配列
    $ch_list = array();
    // Curlハンドルの用意と、マルチハンドルへの登録
    foreach ($url_list as $url) {
        $ch_list[$url] = curl_init($url);
        curl_setopt($ch_list[$url], CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch_list[$url], CURLOPT_TIMEOUT, 1);
        // タイムアウト秒数を指定
        curl_setopt($ch_list[$url], CURLOPT_SSL_VERIFYPEER, false);
        curl_multi_add_handle($mh, $ch_list[$url]);
    }
    // 一括で通信実行、全て終わるのを待つ
    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while ($running);
    // 実行結果の取得
    foreach ($url_list as $url) {
        // ステータスとコンテンツ内容の取得
        $results[$url] = curl_getinfo($ch_list[$url]);
        $results[$url]["content"] = curl_multi_getcontent($ch_list[$url]);
        // Curlハンドルの後始末
        curl_multi_remove_handle($mh, $ch_list[$url]);
        curl_close($ch_list[$url]);
    }
    // マルチハンドルの後始末
    curl_multi_close($mh);
    // 結果返却
    return $results;
}
Example #2
0
 function curl_multi_request($query_arr, $data, $method = 'POST')
 {
     $ch = curl_multi_init();
     $count = count($query_arr);
     $ch_arr = array();
     for ($i = 0; $i < $count; $i++) {
         $query_string = $query_arr[$i];
         $ch_arr[$i] = curl_init($query_string);
         curl_setopt($ch_arr[$i], CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch_arr[$i], CURLOPT_POST, 1);
         curl_setopt($ch_arr[$i], CURLOPT_POSTFIELDS, $data);
         // post 提交方式
         curl_multi_add_handle($ch, $ch_arr[$i]);
     }
     $running = null;
     do {
         curl_multi_exec($ch, $running);
     } while ($running > 0);
     for ($i = 0; $i < $count; $i++) {
         $results[$i] = curl_multi_getcontent($ch_arr[$i]);
         curl_multi_remove_handle($ch, $ch_arr[$i]);
     }
     curl_multi_close($ch);
     return $results;
 }
Example #3
0
 function _multifetch($urls)
 {
     // shhsecret = "Welcome to ServerFault"
     $mh = curl_multi_init();
     $handles = array();
     foreach (array_keys($urls) as $urlkey) {
         $ch = curl_init($urls[$urlkey]);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         //curl_setopt($ch, CURLOPT_HEADER, TRUE);
         //curl_setopt($ch, CURLOPT_COOKIE, 'shhsecret="Welcome to SuperUser"');
         curl_setopt($ch, CURLOPT_USERAGENT, "http://meta.stackoverflow.com/questions/9863/track-your-reputation");
         curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
         $handles[$urlkey] = $ch;
         curl_multi_add_handle($mh, $ch);
     }
     do {
         curl_multi_exec($mh, $active);
     } while ($active > 0);
     $output = array();
     foreach (array_keys($handles) as $hk) {
         $status = curl_getinfo($handles[$hk], CURLINFO_HTTP_CODE);
         if ($status != 200) {
             $output[$hk] = $status;
         } else {
             $output[$hk] = curl_multi_getcontent($handles[$hk]);
         }
     }
     return $output;
 }
 /**
  * Performs multiple (array of RemoteContentRequest) requests and fills in the responses
  * in the $request objects
  *
  * @param Array of RemoteContentRequest's $requests
  * @return $requests
  */
 public function multiFetchRequest(array $requests)
 {
     $mh = curl_multi_init();
     foreach ($requests as $request) {
         $request->handle = $this->initCurlHandle($request->getUrl());
         // Set this so the multihandler will return data
         curl_setopt($request->handle, CURLOPT_RETURNTRANSFER, 1);
         $this->setHeaders($request);
         curl_multi_add_handle($mh, $request->handle);
     }
     $running = null;
     do {
         curl_multi_exec($mh, $running);
     } while ($running > 0);
     foreach ($requests as $request) {
         // Execute the request
         $content = curl_multi_getcontent($request->handle);
         $this->parseResult($request, $content);
         curl_multi_remove_handle($mh, $request->handle);
         unset($request->handle);
     }
     curl_multi_close($mh);
     unset($mh);
     return $requests;
 }
Example #5
0
 public static function multi_get_urls(&$urls)
 {
     if (count($urls) <= 0) {
         return false;
     }
     $harr = array();
     //handle array
     foreach ($urls as $k => $v) {
         $h = curl_init();
         curl_setopt($h, CURLOPT_URL, $v['url']);
         curl_setopt($h, CURLOPT_HEADER, 0);
         curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
         array_push($harr, $h);
     }
     $mh = curl_multi_init();
     foreach ($harr as $k => $v) {
         curl_multi_add_handle($mh, $v);
     }
     $running = null;
     do {
         curl_multi_exec($mh, $running);
     } while ($running > 0);
     // get the result and save it in the result ARRAY
     foreach ($harr as $k => $h) {
         $urls[$k]['data'] = curl_multi_getcontent($h);
     }
     // close all the connections
     foreach ($harr as $k => $v) {
         curl_multi_remove_handle($mh, $v);
     }
     curl_multi_close($mh);
     return true;
 }
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequests(array $internalRequests, $success, $error)
 {
     $curlMulti = curl_multi_init();
     $contexts = array();
     foreach ($internalRequests as $internalRequest) {
         $contexts[] = array('curl' => $curl = $this->createCurl($internalRequest), 'request' => $internalRequest);
         curl_multi_add_handle($curlMulti, $curl);
     }
     do {
         do {
             $exec = curl_multi_exec($curlMulti, $running);
         } while ($exec === CURLM_CALL_MULTI_PERFORM);
         while ($done = curl_multi_info_read($curlMulti)) {
             $curl = $done['handle'];
             $internalRequest = $this->resolveInternalRequest($curl, $contexts);
             try {
                 $response = $this->createResponse($curl, curl_multi_getcontent($curl), $internalRequest);
                 $response = $response->withParameter('request', $internalRequest);
                 call_user_func($success, $response);
             } catch (HttpAdapterException $e) {
                 $e->setRequest($internalRequest);
                 call_user_func($error, $e);
             }
             curl_multi_remove_handle($curlMulti, $curl);
             curl_close($curl);
         }
     } while ($running);
     curl_multi_close($curlMulti);
 }
 /**
  * @param string[] $urls
  * @return string[]
  */
 public function multiGet(array $urls)
 {
     $curlHandlers = [];
     $multiHandler = curl_multi_init();
     foreach ($urls as $url) {
         $currentHandler = curl_init($url);
         $curlHandlers[$url] = $currentHandler;
         curl_setopt($currentHandler, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($currentHandler, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT);
         curl_multi_add_handle($multiHandler, $currentHandler);
     }
     $isStillRunning = null;
     do {
         curl_multi_exec($multiHandler, $isStillRunning);
         sleep(self::LOAD_WAIT_TIME);
     } while ($isStillRunning);
     $results = [];
     // get content and remove handles
     foreach ($urls as $url) {
         $currentHandler = $curlHandlers[$url];
         $results[$url] = curl_multi_getcontent($currentHandler);
         curl_multi_remove_handle($multiHandler, $currentHandler);
     }
     // all done
     curl_multi_close($multiHandler);
     return $results;
 }
Example #8
0
 /** Execute request or get its response
  * @param string request identifier
  * @param array array(string $url) for executing request, array() for getting response
  * @return mixed
  */
 function __call($name, array $args)
 {
     if ($args) {
         // execute request
         list($url) = $args;
         $curl = curl_init($url);
         $this->curl[$name] = $curl;
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
         $return = curl_multi_add_handle($this->multi, $curl);
         while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM) {
         }
         return $return;
     }
     // get response
     if (!isset($this->curl[$name])) {
         // wrong identifier
         return false;
     }
     $curl = $this->curl[$name];
     while (!isset($this->done[(int) $curl])) {
         curl_multi_select($this->multi, $this->timeout);
         while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM) {
         }
         while ($info = curl_multi_info_read($this->multi)) {
             if ($info["msg"] == CURLMSG_DONE) {
                 $this->done[(int) $info["handle"]] = true;
             }
         }
     }
     return curl_multi_getcontent($curl);
 }
Example #9
0
 function multi_request($urls)
 {
     $curly = array();
     $result = array();
     $mh = curl_multi_init();
     foreach ($urls as $id => $url) {
         $curly[$id] = curl_init();
         curl_setopt($curly[$id], CURLOPT_URL, $url);
         curl_setopt($curly[$id], CURLOPT_HEADER, 0);
         curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curly[$id], CURLOPT_TIMEOUT, 30);
         curl_setopt($curly[$id], CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($curly[$id], CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($curly[$id], CURLOPT_SSL_VERIFYHOST, 0);
         curl_multi_add_handle($mh, $curly[$id]);
     }
     $running = null;
     do {
         curl_multi_exec($mh, $running);
     } while ($running > 0);
     foreach ($curly as $id => $c) {
         $result[$id] = curl_multi_getcontent($c);
         curl_multi_remove_handle($mh, $c);
     }
     curl_multi_close($mh);
     return $result;
 }
 private function checkForCompletedRequests()
 {
     do {
         $mrc = curl_multi_exec($this->multi_handle, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         if (curl_multi_select($this->multi_handle) != -1) {
             do {
                 $mrc = curl_multi_exec($this->multi_handle, $active);
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         } else {
             return;
         }
     }
     // Now grab the information about the completed requests
     while ($info = curl_multi_info_read($this->multi_handle)) {
         $ch = $info['handle'];
         $ch_array_key = (int) $ch;
         if (!isset($this->outstanding_requests[$ch_array_key])) {
             die("Error - handle wasn't found in requests: '{$ch}' in " . print_r($this->outstanding_requests, true));
         }
         $request = $this->outstanding_requests[$ch_array_key];
         $url = $request['url'];
         $content = curl_multi_getcontent($ch);
         $callback = $request['callback'];
         $user_data = $request['user_data'];
         call_user_func($callback, $content, $url, $ch, $user_data);
         unset($this->outstanding_requests[$ch_array_key]);
         curl_multi_remove_handle($this->multi_handle, $ch);
     }
 }
Example #11
0
function mult_post($connomains)
{
    $mh = curl_multi_init();
    foreach ($connomains as $i => $url) {
        $postData = "------WebKitFormBoundaryy9zMscQCwUGxoejM\r\n";
        $postData .= "Content-Disposition: form-data; name=\"urllink\"\r\n\r\n";
        $postData .= $url . "\r\n";
        $postData .= "------WebKitFormBoundaryy9zMscQCwUGxoejM\r\n";
        $postData .= "Content-Disposition: form-data; name=\"url-2txt\"\r\n\r\n\r\n\r\n";
        $postData .= "------WebKitFormBoundaryy9zMscQCwUGxoejM--\r\n";
        $post_url = "http://deeplearning.cs.toronto.edu//api/url.php";
        $header = array("Host: deeplearning.cs.toronto.edu", "Content-Length: " . strlen($postData), 'X-Requested-With: XMLHttpRequest', 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryy9zMscQCwUGxoejM', 'Accept-Language: zh-CN,zh;q=0.8', 'X-Requested-With: XMLHttpRequest', 'X-Requested-With: XMLHttpRequest');
        $conn[$i] = curl_init('http://deeplearning.cs.toronto.edu//api/url.php');
        // $res= curl_setopt ($mh, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($conn[$i], CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($conn[$i], CURLOPT_HEADER, 0);
        curl_setopt($conn[$i], CURLOPT_POST, 1);
        curl_setopt($conn[$i], CURLOPT_POSTFIELDS, $postData);
        curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($conn[$i], CURLOPT_HTTPHEADER, $header);
        curl_multi_add_handle($mh, $conn[$i]);
    }
    do {
        $n = curl_multi_exec($mh, $active);
    } while ($active);
    foreach ($connomains as $i => $urlwhole) {
        $pattern = '#Sentence:</h4><ul><li>(.*)</li></ul><br/><h4>#';
        $res[$i] = getPatternResult($pattern, curl_multi_getcontent($conn[$i]));
        curl_close($conn[$i]);
    }
    // print_r($res);
    return $res;
}
 public function ApiFindMymovies()
 {
     $message = "je suis vide";
     $mymovies = $this->em->getRepository('FilmBundle:Moviesfavorite')->FindMymovies($this->user->getid());
     $ch = array();
     $responses = array();
     $mh = curl_multi_init();
     foreach ($mymovies as $key => $value) {
         $ch[$key] = curl_init("http://api.themoviedb.org/3/movie/" . $value->getIdmovies() . "?api_key=c7eb74475a1e3d62a7ab838d184bf0a7&language=fr");
         curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
         curl_multi_add_handle($mh, $ch[$key]);
     }
     $running = null;
     do {
         curl_multi_exec($mh, $running);
     } while ($running);
     foreach ($mymovies as $key => $value) {
         $data = curl_multi_getcontent($ch[$key]);
         $responses[$key] = $data;
     }
     curl_multi_close($mh);
     if (is_array($responses)) {
         return $responses;
     } else {
         return $message;
     }
 }
Example #13
0
 protected function longurl_api($url, $options = array())
 {
     if (empty($options['format'])) {
         $options['format'] = 'php';
     }
     $options['user-agent'] = empty($options['user-agent']) ? "LongURL-PHP-Client-Library/0.1" : $options['user-agent'];
     // construct the query
     $queries = array();
     if (is_array($options)) {
         $queries = array_merge($queries, $options);
     }
     $query = http_build_query($queries);
     $url .= '?' . $query;
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_USERAGENT, $options['user-agent']);
     curl_exec($ch);
     $longurl_return = array();
     $longurl_return['headers'] = curl_getinfo($ch);
     $content = curl_multi_getcontent($ch);
     if ($options['format'] == 'php') {
         $longurl_return['content'] = unserialize($content);
     } else {
         $longurl_return['content'] = $content;
     }
     return $longurl_return;
 }
 public function multiCurlPoll()
 {
     $curl_mh = $this->curl_mh;
     do {
         $mrc = curl_multi_exec($curl_mh, $still_running);
         if ($mrc == CURLM_OK) {
             $info = curl_multi_info_read($curl_mh);
             if ($info && isset($this->curl_cb[(int) $info['handle']])) {
                 $as = $this->curl_cb[(int) $info['handle']];
                 $curl = $info['handle'];
                 unset($this->curl_cb[(int) $curl]);
                 $as->_futoin_response = curl_multi_getcontent($curl);
                 $as->success($curl, $info);
                 curl_multi_remove_handle($this->curl_mh, $curl);
             }
         }
     } while ($mrc === CURLM_CALL_MULTI_PERFORM);
     if ($this->curl_event !== null) {
         \FutoIn\RI\AsyncTool::cancelCall($this->curl_event);
         $this->curl_event = null;
     }
     if ($this->curl_cb) {
         $this->curl_event = \FutoIn\RI\AsyncTool::callLater([$this, "multiCurlSelect"]);
     }
 }
Example #15
0
 /**
  * Executa todos os navegadores fazendo as requisições
  * @return void 
  */
 public function execute()
 {
     if ($this->countRequests() == 0) {
         return false;
     }
     // create the multiple cURL handle
     $mh = curl_multi_init();
     // add the two handles
     foreach ($this->Browsers as $Browser) {
         $Browser->prepare();
         curl_multi_add_handle($mh, $Browser->getCurlResource());
     }
     $active = null;
     // execute the handles
     // execute the handles
     do {
         $n = curl_multi_exec($mh, $active);
         usleep(800);
     } while ($active);
     // close the handles
     foreach ($this->Browsers as $label => $Browser) {
         curl_multi_remove_handle($mh, $Browser->getCurlResource());
         $this->Browsers[$label]->processResult(curl_multi_getcontent($Browser->getCurlResource()));
     }
     curl_multi_close($mh);
 }
Example #16
0
 /**
  * 异步http请求
  * @param $method
  * @param $url
  * @param array $data
  * @param int $connectTimeout
  * @param int $timeout
  * @param null $callback
  * @param null $header
  * @param string $proxy
  * @param bool $needHeader
  */
 public function sendAsyncRequest($method, $url, $data = array(), $callback = null, $header = array(), $proxy = '', $needHeader = false, $connectTimeout = 5, $timeout = 5)
 {
     if (!$this->mh) {
         $this->mh = curl_multi_init();
     }
     $ch = $this->initCurl($method, $url, $data, $connectTimeout, $timeout, $header, $proxy, $needHeader);
     curl_multi_add_handle($this->mh, $ch);
     //开始执行
     do {
         $mrc = curl_multi_exec($this->mh, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($done = curl_multi_info_read($this->mh)) {
         $result = $done['result'];
         $ch = $done['handle'];
         if ($result === CURLE_OK) {
             //请求正常返回
             if (is_callable($callback)) {
                 $resp = curl_multi_getcontent($ch);
                 call_user_func($callback, $resp);
             }
         }
         curl_multi_remove_handle($this->mh, $ch);
         curl_close($ch);
     }
 }
 /**
  * Executes a curl request.
  *
  * @param  resource $request
  * @return \Frlnc\Slack\Contracts\Http\Response
  */
 public function executeMultiRequest($multiRequest, $singleRequests)
 {
     $responses = [];
     $infos = [];
     $active = null;
     do {
         $status = curl_multi_exec($multiRequest, $active);
         $infos[] = curl_multi_info_read($multiRequest);
     } while ($status === CURLM_CALL_MULTI_PERFORM || $active);
     foreach ($singleRequests as $index => $singleRequest) {
         $body = curl_multi_getcontent($singleRequest);
         curl_multi_remove_handle($multiRequest, $singleRequest);
         curl_close($singleRequest);
         $info = $infos[$index];
         $statusCode = $info['http_code'];
         $headers = $info['request_header'];
         if (function_exists('http_parse_headers')) {
             $headers = http_parse_headers($headers);
         } else {
             $header_text = substr($headers, 0, strpos($headers, "\r\n\r\n"));
             $headers = [];
             foreach (explode("\r\n", $header_text) as $i => $line) {
                 if ($i !== 0) {
                     list($key, $value) = explode(': ', $line);
                     $headers[$key] = $value;
                 }
             }
         }
         $responses[] = $this->factory->build($body, $headers, $statusCode);
     }
     curl_multi_close($multiRequest);
     return $responses;
 }
Example #18
0
 public function ApiDetailsmovies()
 {
     $response = array();
     $id = $this->request->getCurrentRequest()->get('id');
     $ch_1 = curl_init("http://api.themoviedb.org/3/movie/" . $id . "?api_key=c7eb74475a1e3d62a7ab838d184bf0a7&language=fr");
     $ch_2 = curl_init("http://api.themoviedb.org/3/movie/" . $id . "/videos?api_key=c7eb74475a1e3d62a7ab838d184bf0a7&language=fr");
     curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);
     // build the multi-curl handle, adding both $ch
     $mh = curl_multi_init();
     curl_multi_add_handle($mh, $ch_1);
     curl_multi_add_handle($mh, $ch_2);
     // execute all queries simultaneously, and continue when all are complete
     $running = null;
     do {
         curl_multi_exec($mh, $running);
     } while ($running);
     //  $result = json_decode($data);
     // all of our requests are done, we can now access the results
     $data_1 = curl_multi_getcontent($ch_1);
     $data_2 = curl_multi_getcontent($ch_2);
     $ispresent = $this->Dejavu($id);
     $whishlist = $this->Wishlist($id);
     $response["ispresent"] = $ispresent;
     $response["wishlist"] = $whishlist;
     $response[] = $data_1;
     $response[] = $data_2;
     return $response;
 }
Example #19
0
 public function flush()
 {
     if (false === ($curlm = curl_multi_init())) {
         throw new ClientException('Unable to create a new cURL multi handle');
     }
     // prepare a cURL handle for each entry in the queue
     foreach ($this->queue as $i => &$queue) {
         list($request, $response, $options) = $queue;
         $curl = $queue[] = static::createCurlHandle();
         $this->prepare($curl, $request, $options);
         curl_multi_add_handle($curlm, $curl);
     }
     $active = null;
     do {
         $mrc = curl_multi_exec($curlm, $active);
     } while (CURLM_CALL_MULTI_PERFORM == $mrc);
     while ($active && CURLM_OK == $mrc) {
         if (-1 != curl_multi_select($curlm)) {
             do {
                 $mrc = curl_multi_exec($curlm, $active);
             } while (CURLM_CALL_MULTI_PERFORM == $mrc);
         }
     }
     // populate the responses
     while (list($request, $response, $options, $curl) = array_shift($this->queue)) {
         static::populateResponse($curl, curl_multi_getcontent($curl), $response);
         curl_multi_remove_handle($curlm, $curl);
     }
     curl_multi_close($curlm);
 }
Example #20
0
 /**
  * @param $mh
  * @param array $results
  * @param array $options
  * @return int
  */
 public static function makeRequests($mh, array &$results, array $options = array())
 {
     // While we're still active, execute curl
     $active = NULL;
     do {
         // Continue to exec until curl is ready to
         // give us more data
         do {
             $mrc = curl_multi_exec($mh, $active);
         } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         // Wait for activity on any curl-connection
         curl_multi_select($mh);
         //do something with the return values
         while (($info = curl_multi_info_read($mh)) !== FALSE) {
             if ($info["result"] == CURLE_OK) {
                 $info['content'] = curl_multi_getcontent($info["handle"]);
             } else {
                 $info['error'] = curl_error($info["handle"]);
             }
             $results[strval($info["handle"])] = $info;
             curl_multi_remove_handle($mh, $info["handle"]);
         }
     } while ($active && $mrc == CURLM_OK);
     curl_multi_close($mh);
     return $mrc;
 }
Example #21
0
File: Util.php Project: guhya/Slim
 /**
  * Perform parallel cURL request.
  *
  * @param array $urls Array of URLs to make request.
  * @param array $options (Optional) Array of additional cURL options.
  * @return mixed Results from the request (if any).
  */
 public function curlMultiRequest($urls, $options = array())
 {
     $ch = array();
     $results = array();
     $mh = curl_multi_init();
     foreach ($urls as $key => $val) {
         $ch[$key] = curl_init();
         if ($options) {
             curl_setopt_array($ch[$key], $options);
         }
         curl_setopt($ch[$key], CURLOPT_URL, $val);
         curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
         curl_multi_add_handle($mh, $ch[$key]);
     }
     $running = null;
     do {
         curl_multi_exec($mh, $running);
     } while ($running > 0);
     // Get content and remove handles.
     foreach ($ch as $key => $val) {
         $results[$key] = curl_multi_getcontent($val);
         curl_multi_remove_handle($mh, $val);
     }
     curl_multi_close($mh);
     return $results;
 }
Example #22
0
 public function execute()
 {
     $mh = curl_multi_init();
     $chs = $this->chs();
     $stillRunning = 0;
     $result = array();
     if (function_exists('curl_multi_setopt')) {
         curl_multi_setopt($mh, CURLMOPT_PIPELINING, 1);
         curl_multi_setopt($mh, CURLMOPT_MAXCONNECTS, $this->maxConnectsSize());
     }
     foreach ($chs as $ch) {
         curl_multi_add_handle($mh, $ch);
     }
     do {
         $execReturnValue = curl_multi_exec($mh, $stillRunning);
         curl_multi_select($mh);
     } while ($stillRunning > 0);
     foreach ($chs as $i => $ch) {
         $curlError = curl_error($ch);
         if ($curlError === "") {
             $result[$i] = curl_multi_getcontent($ch);
         } else {
             throw new PubnubException("Curl error on handle {$i}: {$curlError}\n");
         }
         curl_multi_remove_handle($mh, $ch);
         curl_close($ch);
     }
     curl_multi_close($mh);
     $this->requests = array();
     if ($execReturnValue != CURLM_OK) {
         throw new PubnubException(curl_multi_strerror($execReturnValue));
     }
     return $result;
 }
Example #23
0
 /**
  * Php multi curl wrapper
  * @param  array $urls
  * @return array
  */
 public function multiCurl(array $urls)
 {
     // array of curl handles
     $handles = array();
     // data to be returned
     $result = array();
     // multi handle
     $mh = curl_multi_init();
     // loop through $data and create curl handles
     // then add them to the multi-handle
     foreach ($urls as $k => $u) {
         $handles[$k] = curl_init();
         curl_setopt($handles[$k], CURLOPT_URL, $u);
         curl_setopt($handles[$k], CURLOPT_HTTPHEADER, array('Accept-Language:en;q=0.8,en-US;q=0.6'));
         curl_setopt($handles[$k], CURLOPT_RETURNTRANSFER, 1);
         curl_multi_add_handle($mh, $handles[$k]);
     }
     // execute the handles
     $running = null;
     do {
         curl_multi_exec($mh, $running);
     } while ($running > 0);
     // get content and remove handles
     foreach ($handles as $id => $content) {
         $results[$id] = curl_multi_getcontent($content);
         curl_multi_remove_handle($mh, $content);
     }
     // all done
     curl_multi_close($mh);
     return $this->removeScripts($results);
 }
Example #24
0
 /**
  * @return $this
  */
 public function multiRequest($delay = null)
 {
     /*Reset response collection with empty array*/
     $this->respCollection = array();
     $this->chMulti = curl_multi_init();
     foreach ($this->urls as $k => $v) {
         $this->cHandlers[$k] = curl_init();
         $this->options[CURLOPT_URL] = $v;
         curl_setopt_array($this->cHandlers[$k], $this->options);
         curl_multi_add_handle($this->chMulti, $this->cHandlers[$k]);
     }
     $prev = $current = null;
     do {
         curl_multi_exec($this->chMulti, $current);
         if ($current < $prev) {
             $mInfo = curl_multi_info_read($this->chMulti);
             if ($mInfo) {
                 $executed = $mInfo["handle"];
                 $info = curl_getinfo($executed);
                 $content = curl_multi_getcontent($executed);
                 if ($content) {
                     $this->respCollection[$info["url"]]['requestInfo'] = $info;
                     $this->respCollection[$info["url"]]['content'] = $content;
                 }
             }
         }
         $prev = $current;
         if ($delay) {
             usleep($delay);
         }
     } while ($current > 0);
     return $this;
 }
Example #25
0
 public static function get_multi_tags(array $url, $auth)
 {
     $mh = curl_multi_init();
     $header = 'Authorization: Bearer ' . $auth;
     foreach ($url as $key => $links) {
         for ($i = 0; $i < count($links); $i++) {
             $ch[$key][$i] = curl_init();
             curl_setopt($ch[$key][$i], CURLOPT_URL, "https://api.clarifai.com/v1/tag/?url=" . $links[$i]);
             curl_setopt($ch[$key][$i], CURLOPT_HTTPHEADER, array($header));
             curl_setopt($ch[$key][$i], CURLOPT_RETURNTRANSFER, true);
             //add the two handles
             curl_multi_add_handle($mh, $ch[$key][$i]);
         }
     }
     $running = null;
     do {
         curl_multi_exec($mh, $running);
     } while ($running > 0);
     $ret = array();
     foreach ($url as $key => $links) {
         $tags = array();
         for ($i = 0; $i < count($links); $i++) {
             if (strcmp(json_decode(curl_multi_getcontent($ch[$key][$i]))->status_code, "OK") == 0) {
                 array_push($tags, json_decode(curl_multi_getcontent($ch[$key][$i]))->results[0]->result->tag->classes);
             }
             //close the handles
             curl_multi_remove_handle($mh, $ch[$key][$i]);
         }
         $ret[$key] = $tags;
     }
     curl_multi_close($mh);
     return $ret;
 }
Example #26
0
function reqMultiCurls($urls)
{
    // for storing cUrl handlers
    $chs = array();
    // for storing the reponses strings
    $contents = array();
    // loop through an array of URLs to initiate
    // one cUrl handler for each URL (request)
    foreach ($urls as $url) {
        $ch = curl_init($url);
        // tell cUrl option to return the response
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $chs[] = $ch;
    }
    // initiate a multi handler
    $mh = curl_multi_init();
    // add all the single handler to a multi handler
    foreach ($chs as $key => $ch) {
        curl_multi_add_handle($mh, $ch);
    }
    // execute the multi cUrl handler
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM || $active);
    // retrieve the reponse from each single handler
    foreach ($chs as $key => $ch) {
        if (curl_errno($ch) == CURLE_OK) {
            $contents[] = curl_multi_getcontent($ch);
        } else {
            echo "Err>>> " . curl_error($ch) . "\n";
        }
    }
    curl_multi_close($mh);
    return $contents;
}
Example #27
0
 public function exec()
 {
     $active = null;
     do {
         do {
             $status = curl_multi_exec($this->curlHandle, $active);
         } while ($status == CURLM_CALL_MULTI_PERFORM);
         if ($status != CURLM_OK) {
             break;
         }
         $response = array();
         $respond = curl_multi_info_read($this->curlHandle);
         while ($respond) {
             $callback = $this->requestMap[(string) $respond['handle']];
             $responses[$callback]['content'] = curl_multi_getcontent($respond['handle']);
             $responses[$callback]['httpcode'] = curl_getinfo($respond['handle'], CURLINFO_HTTP_CODE);
             curl_multi_remove_handle($this->curlHandle, $respond['handle']);
             curl_close($respond['handle']);
             $respond = curl_multi_info_read($this->curlHandle);
         }
         if ($active > 0) {
             curl_multi_select($this->curlHandle, 0.05);
         }
     } while ($active);
     return $responses;
 }
Example #28
0
 public function execute()
 {
     $mh = curl_multi_init();
     $conn = [];
     foreach ($this->getUrls() as $k => $url) {
         $this->setOption(CURLOPT_URL, $url);
         $ch = curl_init();
         curl_setopt_array($ch, $this->getOptions());
         $conn[$k] = $ch;
         curl_multi_add_handle($mh, $conn[$k]);
     }
     $active = false;
     do {
         $mrc = curl_multi_exec($mh, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         if (curl_multi_select($mh) != -1) {
             do {
                 $mrc = curl_multi_exec($mh, $active);
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         }
     }
     $res = [];
     foreach ($this->getUrls() as $k => $url) {
         $res[$k] = curl_multi_getcontent($conn[$k]);
         curl_close($conn[$k]);
         curl_multi_remove_handle($mh, $conn[$k]);
     }
     curl_multi_close($mh);
     return $res;
 }
Example #29
0
 /**
  * Make the request given the Request set and content
  *
  * @param \Yubikey\RequestCollection $requests Request collection
  * @return \Yubikey\ResponseCollection instance
  */
 public function request(\Yubikey\RequestCollection $requests)
 {
     $responses = new \Yubikey\ResponseCollection();
     $startTime = microtime(true);
     $multi = curl_multi_init();
     $curls = array();
     foreach ($requests as $index => $request) {
         $curls[$index] = curl_init();
         curl_setopt_array($curls[$index], array(CURLOPT_URL => $request->getUrl(), CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1));
         curl_multi_add_handle($multi, $curls[$index]);
     }
     do {
         while (curl_multi_exec($multi, $active) == CURLM_CALL_MULTI_PERFORM) {
         }
         while ($info = curl_multi_info_read($multi)) {
             if ($info['result'] == CURLE_OK) {
                 $return = curl_multi_getcontent($info['handle']);
                 $cinfo = curl_getinfo($info['handle']);
                 $url = parse_url($cinfo['url']);
                 $response = new \Yubikey\Response(array('host' => $url['host'], 'mt' => microtime(true) - $startTime));
                 $response->parse($return);
                 $responses->add($response);
             }
         }
     } while ($active);
     return $responses;
 }
Example #30
-5
 public function exec()
 {
     $mh = curl_multi_init();
     foreach ($this->url_list as $i => $url) {
         $conn[$i] = curl_init($url);
         curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($conn[$i], CURLOPT_HEADER, 0);
         curl_setopt($conn[$i], CURLOPT_NOBODY, 0);
         curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 0);
         curl_setopt($conn[$i], CURLOPT_TIMEOUT, 30);
         curl_multi_add_handle($mh, $conn[$i]);
     }
     $active = FALSE;
     do {
         $mrc = curl_multi_exec($mh, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         //if(curl_multi_select($mh) != -1){
         do {
             $mrc = curl_multi_exec($mh, $active);
         } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         //}
     }
     $result = array();
     foreach ($this->url_list as $i => $url) {
         $result[$i] = curl_multi_getcontent($conn[$i]);
         curl_close($conn[$i]);
         curl_multi_remove_handle($mh, $conn[$i]);
     }
     curl_multi_close($mh);
     return $result;
 }