/**
 * 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;
}
 /**
  * Get the size of each of the urls in a list
  *
  * @param array $urls
  *
  * @return array
  * @throws \Exception
  */
 public function batch(array $urls)
 {
     $multi = curl_multi_init();
     $results = array();
     // Create the curl handles and add them to the multi_request
     foreach (array_values($urls) as $count => $uri) {
         $results[$uri] = array();
         ${$count} = $this->handle($uri, $results[$uri]);
         $code = curl_multi_add_handle($multi, ${$count});
         if ($code != CURLM_OK) {
             throw new \Exception("Curl handle for {$uri} could not be added");
         }
     }
     // Perform the requests
     do {
         while (($mrc = curl_multi_exec($multi, $active)) == CURLM_CALL_MULTI_PERFORM) {
         }
         if ($mrc != CURLM_OK && $mrc != CURLM_CALL_MULTI_PERFORM) {
             throw new \Exception("Curl error code: {$mrc}");
         }
         if ($active && curl_multi_select($multi) === -1) {
             // Perform a usleep if a select returns -1.
             // See: https://bugs.php.net/bug.php?id=61141
             usleep(250);
         }
     } while ($active);
     // Figure out why individual requests may have failed
     foreach (array_values($urls) as $count => $uri) {
         $error = curl_error(${$count});
         if ($error) {
             $results[$uri]['failure_reason'] = $error;
         }
     }
     return $results;
 }
예제 #3
0
 /**
  * Wait for request(s) to be completed.
  *
  * @param PromiseCore|null $targetCore
  */
 public function wait(PromiseCore $targetCore = null)
 {
     do {
         $status = curl_multi_exec($this->multiHandle, $active);
         $info = curl_multi_info_read($this->multiHandle);
         if (false !== $info) {
             $core = $this->findCoreByHandle($info['handle']);
             if (null === $core) {
                 // We have no promise for this handle. Drop it.
                 curl_multi_remove_handle($this->multiHandle, $info['handle']);
                 continue;
             }
             if (CURLE_OK === $info['result']) {
                 try {
                     $core->fulfill();
                 } catch (\Exception $e) {
                     $core->reject(new RequestException($e->getMessage(), $core->getRequest(), $e));
                 }
             } else {
                 $error = curl_error($core->getHandle());
                 $core->reject(new RequestException($error, $core->getRequest()));
             }
             $this->remove($core);
             // This is a promise we are waited for. So exiting wait().
             if ($core === $targetCore) {
                 return;
             }
         }
     } while ($status === CURLM_CALL_MULTI_PERFORM || $active);
 }
예제 #4
0
 private function executeOneByOne()
 {
     $running = null;
     do {
         curl_multi_exec($this->multiCurl, $running);
     } while ($running > 0);
 }
 /**
  * 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;
 }
예제 #6
0
 /**
  * Execute the requests of all given Curl instances
  *
  * @param Curl[] $instances An array of Curl instances (key = name of instance)
  * @param int $retryCount The maximum number of retries
  * @param int $retryWait Time in seconds to wait between each try
  */
 private function subExec($instances, $retryCount = 0, $retryWait = 0)
 {
     $curlMultiInstance = curl_multi_init();
     foreach ($instances as $name => $curlInstance) {
         curl_multi_add_handle($curlMultiInstance, $curlInstance->getHandle());
     }
     do {
         curl_multi_exec($curlMultiInstance, $stillRunning);
         curl_multi_select($curlMultiInstance);
     } while ($stillRunning);
     for ($retry = 1; $retry <= $retryCount; $retry++) {
         $retryRequired = false;
         foreach ($instances as $curlInstance) {
             if (!$curlInstance->isSuccessful()) {
                 $retryRequired = true;
                 break;
             }
         }
         if (!$retryRequired) {
             break;
         }
         sleep($retryWait);
         $this->retryFailed($instances);
     }
     foreach ($instances as $curlInstance) {
         $curlInstance->setContent(curl_multi_getcontent($curlInstance->getHandle()));
     }
     curl_multi_close($curlMultiInstance);
 }
예제 #7
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;
 }
예제 #8
0
파일: Curl.php 프로젝트: samirios1/niter
 /**
  * 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);
 }
예제 #9
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);
 }
예제 #10
0
  public function getResult($key = null)
  {
    if($key != null)
    {
      if(isset($this->responses[$key]))
      {
        return $this->responses[$key];
      }

      $running = null;
      do
      {
        $resp = curl_multi_exec($this->mc, $runningCurrent);
        if($running !== null && $runningCurrent != $running)
        {
          $this->storeResponses($key);
          if(isset($this->responses[$key]))
          {
            return $this->responses[$key];
          }
        }
        $running = $runningCurrent;
      }while($runningCurrent > 0);
    }

    return false;
  }
예제 #11
0
 /**
  * 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;
 }
예제 #12
0
 private function consumeStream()
 {
     $url = 'https://stream.gnip.com/accounts/' . GNIP_ACCOUNT . '/publishers/' . $this->endpoint . '/streams/track/Prod.json';
     $that = $this;
     $callback = function ($ch, $data) use($that) {
         $chunk = trim($data);
         if (!empty($chunk)) {
             $that->handleChunk($chunk);
         }
         return strlen($data);
     };
     $ch = curl_init();
     curl_setopt_array($ch, array(CURLOPT_URL => $url, CURLOPT_ENCODING => 'gzip', CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => GNIP_USERNAME . ':' . GNIP_PASSWORD, CURLOPT_WRITEFUNCTION => $callback, CURLOPT_BUFFERSIZE => 2000, CURLOPT_LOW_SPEED_LIMIT => 1, CURLOPT_LOW_SPEED_TIME => 60));
     $running = null;
     $mh = curl_multi_init();
     curl_multi_add_handle($mh, $ch);
     // the event loop
     do {
         curl_multi_select($mh, 1);
         // wait for activity
         curl_multi_exec($mh, $running);
         // perform activity
     } while ($running > 0);
     curl_multi_remove_handle($mh, $ch);
     curl_multi_close($mh);
 }
예제 #13
0
 public function run()
 {
     // if sth. goes wrong with init (no requests), return false.
     if (!($mh = $this->initMultiHandle())) {
         return false;
     }
     $active = 0;
     do {
         do {
             $mrc = curl_multi_exec($mh, $active);
         } while (CURLM_CALL_MULTI_PERFORM === $mrc);
         switch ($mrc) {
             case CURLM_OK:
                 break;
             case CURLM_OUT_OF_MEMORY:
                 die('CURL out of memory.');
                 break;
             case CURLM_INTERNAL_ERROR:
                 ezcLog::getInstance()->log('CURL_INTERNAL ERROR', ezcLog::FATAL);
                 break;
         }
         // Did sth. happen? Did a handle finish?
         $moreMessages = 0;
         do {
             $this->handleMultiMessage(curl_multi_info_read($mh, $moreMessages));
         } while ($moreMessages);
         // wait for sth. to do
         if (-1 === curl_multi_select($mh)) {
             ezcLog::getInstance()->log('curl_multi_select returned -1', ezcLog::FATAL);
             $active = false;
             // break the loop
         }
     } while ($active);
     return TRUE;
 }
예제 #14
0
 /**
  * Ping web services
  * 
  * @param string $sitemap Full website path to sitemap
  * @return array Service key with the HTTP response code as the value.
  */
 public static function ping($sitemap)
 {
     // Main handle
     $master = curl_multi_init();
     // List of URLs to ping
     $URLs = Kohana::config('sitemap.ping');
     $handles = array();
     // Create handles for each URL and add them to the main handle.
     foreach ($URLs as $key => $val) {
         $handles[$key] = curl_init(sprintf($val, $sitemap));
         curl_setopt($handles[$key], CURLOPT_FOLLOWLOCATION, TRUE);
         curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, TRUE);
         curl_setopt($handles[$key], CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3');
         curl_multi_add_handle($master, $handles[$key]);
     }
     do {
         curl_multi_exec($master, $still_running);
     } while ($still_running > 0);
     $info = array();
     // Build an array of the execution information.
     foreach (array_keys($URLs) as $key) {
         $info[$key] = curl_getinfo($handles[$key], CURLINFO_HTTP_CODE);
         // Close the handles while we're here.
         curl_multi_remove_handle($master, $handles[$key]);
     }
     // and finally close the master handle.
     curl_multi_close($master);
     return $info;
 }
예제 #15
0
 private function exec_handle($handle)
 {
     $flag = null;
     do {
         curl_multi_exec($handle, $flag);
     } while ($flag > 0);
 }
예제 #16
0
 private function _multi_curl($urls)
 {
     $num = count($urls);
     for ($i = 0; $i < $num; $i++) {
         $ch[] = curl_init();
         // 设置URL和相应的选项
         curl_setopt($ch[$i], CURLOPT_URL, $urls[$i]);
         curl_setopt($ch[$i], CURLOPT_HEADER, 0);
     }
     // 创建批处理cURL句柄
     $mh = curl_multi_init();
     // 增加2个句柄
     for ($i = 0; $i < $num; $i++) {
         curl_multi_add_handle($mh, $ch[$i]);
     }
     $running = null;
     // 执行批处理句柄
     do {
         //usleep ( 10000 );
         $fetch[] = curl_multi_exec($mh, $running);
     } while ($running > 0);
     // 关闭全部句柄
     for ($i = 0; $i < $num; $i++) {
         curl_multi_remove_handle($mh, $ch[$i]);
     }
     curl_multi_close($mh);
     return $fetch;
 }
예제 #17
0
 /**
  * {@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);
 }
예제 #18
0
 /**
  * @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;
 }
예제 #19
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;
 }
예제 #20
0
파일: Util.php 프로젝트: 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;
 }
예제 #21
0
 /**
  * Execute multi curl of this Streamer
  *
  * @return boolean
  * @throws Exception
  */
 public function exec()
 {
     if (!$this->isResource()) {
         throw new Exception("Is not a valid cURL Multi Handle resource", Exception::INVALID_MULTI_CURL);
     }
     if (empty($this->streams)) {
         throw new Exception("Pull of streams is empty", Exception::PULL_IS_EMPTY);
     }
     $running = $messages = 0;
     do {
         // executing...
         if (($error = curl_multi_exec($this->curl, $running)) != 0) {
             throw new Exception(curl_multi_strerror($error), Exception::MULTI_CURL_ERROR);
         }
         // we have some completed streams in this iteration
         do {
             if ($read = curl_multi_info_read($this->curl, $messages)) {
                 $handle = $read['handle'];
                 /** @var $stream Stream */
                 $stream = $this->streams[(int) $handle];
                 $stream->setResponse($read['result'], curl_multi_getcontent($handle));
             }
         } while ($messages);
         // in god we trust...
         usleep(1000);
     } while ($running);
     // close descriptors
     $this->closeResource();
     return $this;
 }
예제 #22
0
 public function update()
 {
     if (!$this->setting_list['active']) {
         return;
     }
     while (count($this->connection_list) < $this->setting_list['max_connections'] && count($this->job_list) > 0) {
         $job = array_shift($this->job_list);
         $host = $job['request']->get_option(CURLOPT_URL);
         var_dump($host);
         if (!$host) {
             return $job['callback'](null);
         }
         if (strpos($host, 'http') !== 0) {
             $job['request']->set_option(CURLOPT_URL, 'http://' . $host);
         }
         $host = parse_url($job['request']->get_option(CURLOPT_URL), PHP_URL_HOST);
         // check if the domain is bad and will block multicurl
         if (!$this->is_host_active($host)) {
             var_dump('wtf');
             if ($job['callback'] != null) {
                 if (phpversion() >= 5.3) {
                     $job['callback'](null);
                 } else {
                     call_user_func_array($job['callback'], array(null));
                 }
             }
             continue;
         }
         $this->connection_list[$job['handle']] = array('request' => $job['request'], 'handle' => $job['handle'], 'callback' => $job['callback']);
         curl_multi_add_handle($this->mc, $job['handle']);
     }
     while (($status = curl_multi_exec($this->mc, $running)) == CURLM_CALL_MULTI_PERFORM) {
         continue;
     }
     if ($status != CURLM_OK) {
         return;
     }
     while ($item = curl_multi_info_read($this->mc)) {
         usleep(20000);
         $handle = $item['handle'];
         $connection = $this->connection_list[$handle];
         $info = curl_getinfo($handle);
         $data = curl_multi_getcontent($handle);
         curl_multi_remove_handle($this->mc, $handle);
         unset($this->connection_list[$handle]);
         $response = new \classes\curl\response();
         $response->request = $connection['request'];
         $response->data = $data;
         $response->info = $info;
         $response->status_code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
         $this->last_response = $response;
         if ($connection['callback'] != null) {
             if (phpversion() >= 5.3) {
                 $connection['callback']($response);
             } else {
                 call_user_func_array($connection['callback'], array($response));
             }
         }
     }
 }
function checkForClosedFilePointer($curl_option, $description)
{
    $fp = fopen(dirname(__FILE__) . '/bug48203.tmp', 'w');
    $ch1 = curl_init();
    $ch2 = curl_init();
    $options = array(CURLOPT_RETURNTRANSFER => 1, $curl_option => $fp, CURLOPT_URL => curl_cli_server_start());
    // we also need to set CURLOPT_VERBOSE to test CURLOPT_STDERR properly
    if (CURLOPT_STDERR == $curl_option) {
        $options[CURLOPT_VERBOSE] = 1;
    }
    if (CURLOPT_INFILE == $curl_option) {
        $options[CURLOPT_UPLOAD] = 1;
    }
    curl_setopt_array($ch1, $options);
    curl_setopt_array($ch2, $options);
    fclose($fp);
    // <-- premature close of $fp caused a crash!
    $mh = curl_multi_init();
    curl_multi_add_handle($mh, $ch1);
    curl_multi_add_handle($mh, $ch2);
    $active = 0;
    do {
        curl_multi_exec($mh, $active);
    } while ($active > 0);
    curl_multi_remove_handle($mh, $ch1);
    curl_multi_remove_handle($mh, $ch2);
    curl_multi_close($mh);
    echo "Ok for {$description}\n";
}
예제 #24
0
파일: Curl.php 프로젝트: AlexBGD/DrKaske
 public function get($url_mixed, $data = array())
 {
     if (is_array($url_mixed)) {
         $curl_multi = curl_multi_init();
         $this->_multi_parent = true;
         $this->curls = array();
         foreach ($url_mixed as $url) {
             $curl = new Curl();
             $curl->_multi_child = true;
             $curl->setOpt(CURLOPT_URL, $this->_buildURL($url, $data), $curl->curl);
             $curl->setOpt(CURLOPT_HTTPGET, true);
             $this->_call($this->_before_send, $curl);
             $this->curls[] = $curl;
             $curlm_error_code = curl_multi_add_handle($curl_multi, $curl->curl);
             if (!($curlm_error_code === CURLM_OK)) {
                 //throw new \ErrorException('cURL multi add handle error: ' .curl_multi_strerror($curlm_error_code));
             }
         }
         foreach ($this->curls as $ch) {
             foreach ($this->_options as $key => $value) {
                 $ch->setOpt($key, $value);
             }
         }
         do {
             $status = curl_multi_exec($curl_multi, $active);
         } while ($status === CURLM_CALL_MULTI_PERFORM || $active);
         foreach ($this->curls as $ch) {
             $this->exec($ch);
         }
     } else {
         $this->setopt(CURLOPT_URL, $this->_buildURL($url_mixed, $data));
         $this->setopt(CURLOPT_HTTPGET, true);
         return $this->exec();
     }
 }
예제 #25
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;
 }
예제 #26
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);
 }
예제 #27
0
 public function Run($echo = true)
 {
     if (!is_array($this->threads)) {
         return false;
     }
     $session = serialize($_SESSION);
     session_write_close();
     //Start
     $cmh = curl_multi_init();
     $tasks = array();
     foreach ($this->threads as $i => $thread) {
         $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
         curl_setopt($ch, CURLOPT_HTTPHEADER, array('PHPThreads: true'));
         curl_setopt($ch, CURLOPT_POST, 1);
         $Post = array('PHPThreads_Run' => base64_encode($this->strcode($thread[0], $this->password)), 'PHPThreads_Vars' => base64_encode($this->strcode($thread[1], $this->password)), 'PHPThreads_Session' => base64_encode($this->strcode($session, $this->password)));
         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($Post));
         $tasks[$i] = $ch;
         curl_multi_add_handle($cmh, $ch);
     }
     $active = null;
     do {
         $mrc = curl_multi_exec($cmh, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         if (curl_multi_select($cmh) != -1) {
             do {
                 $mrc = curl_multi_exec($cmh, $active);
                 $info = curl_multi_info_read($cmh);
                 if ($info['msg'] == CURLMSG_DONE) {
                     $ch = $info['handle'];
                     $url = array_search($ch, $tasks);
                     $result = curl_multi_getcontent($ch);
                     $curl_result = json_decode($result, true);
                     if ($echo) {
                         echo $curl_result['echo'];
                     }
                     $resp[$url] = $curl_result['return'];
                     curl_multi_remove_handle($cmh, $ch);
                     curl_close($ch);
                 }
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         }
     }
     curl_multi_close($cmh);
     session_start();
     $this->Clear();
     //Clear Threads after run
     if (is_array($resp)) {
         ksort($resp);
     }
     return $resp;
     // End
 }
예제 #28
0
 public function getResult($key = null)
 {
     if ($key != null) {
         if (isset($this->responses[$key]['data'])) {
             return $this->responses[$key];
         }
         $innerSleepInt = $outerSleepInt = 1;
         while ($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) {
             usleep($outerSleepInt);
             $outerSleepInt *= $this->sleepIncrement;
             $ms = curl_multi_select($this->mc);
             if ($ms >= CURLM_CALL_MULTI_PERFORM) {
                 do {
                     $this->execStatus = curl_multi_exec($this->mc, $this->running);
                     usleep($innerSleepInt);
                     $innerSleepInt *= $this->sleepIncrement;
                 } while ($this->execStatus == CURLM_CALL_MULTI_PERFORM);
                 $innerSleepInt = 1;
             }
             $this->storeResponses();
             if (isset($this->responses[$key]['data'])) {
                 return $this->responses[$key];
             }
             $runningCurrent = $this->running;
         }
         return null;
     }
     return false;
 }
예제 #29
0
파일: Curl.php 프로젝트: xiaojiays/curl
 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;
 }
예제 #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;
 }