/** * 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; }
/** * 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); }
/** * {@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); }
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(); } }
protected function addRequest($url, $post_data, $header = null, $method = 'POST', $timeout = 0) { if ($this->multi_handle === null) { $this->multi_handle = curl_multi_init(); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if ($post_data) { $post_string = $this->createPostString($post_data); if (isset($post_string[0])) { curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); } } curl_setopt_array($ch, self::$default_options); if ($method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); } elseif ($method == 'GET') { curl_setopt($ch, CURLOPT_HTTPGET, 1); } else { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); } if ($timeout > 0) { curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); } if ($header && is_array($header)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } curl_multi_add_handle($this->multi_handle, $ch); $this->handles[$url] = $ch; }
private function _request($method, $url, $data, $pattern, $timeout, $headers) { $j = time() * 6; $curls = array(); for ($i = 0; $i < 5 && $i < count($this->m_proxies); $i++) { $curl = curl_init($url); $this->_setopt($method, $curl, $data ? $data : null, $headers); // set proxy $host = $this->m_proxies[($j + $i) % count($this->m_proxies)]; $proxy = split(":", $host); if (count($proxy) > 1) { curl_setopt($curl, CURLOPT_PROXYPORT, $proxy[1]); } else { curl_setopt($curl, CURLOPT_PROXYPORT, 80); } curl_setopt($curl, CURLOPT_PROXY, $proxy[0]); curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); curl_multi_add_handle($this->m_multi, $curl); $curls[] = $curl; } $r = $this->_perform($this->m_multi, $timeout, $pattern); foreach ($curls as $curl) { curl_multi_remove_handle($this->m_multi, $curl); curl_close($curl); } return $r; }
/** * 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); }
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; }
/** * @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; }
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; }
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); }
/** * @param $uris * * @throws Exception * @return array */ public function batch($uris) { $multi = curl_multi_init(); foreach ($uris as $uri) { $this->handles[$uri] = $this->getCurlHandle($uri); $code = curl_multi_add_handle($multi, $this->handles[$uri]); if ($code != CURLM_OK) { throw new Exception("Curl handle for {$uri} could not be added"); } } 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); $results = array(); foreach ($uris as $uri) { $results[$uri] = curl_multi_getcontent($this->handles[$uri]); } return $results; }
public function curlMultiAddHandle(&$curlHandle) { if ($this->checkCurlHandle($curlHandle)) { $this->curlHandles[] = $curlHandle; return(curl_multi_add_handle($this->curlMultiHandle, $curlHandle)); } else throw new Exception('Resource is not a curl resource'); // MESSAGE_RU: Ресурс не является curl ресурсом }
/** * Add a normal cURL handle to a cURL multi handle * * @param Curl|resource $ch * @return integer */ public function add_handle($ch) { if ($ch instanceof \Curl) { $ch = $ch->resource(); } return curl_multi_add_handle($this->_mh, $ch); }
/** * 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; }
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; }
public function connect() { $this->server = $this->getConfig('server'); $this->authtoken = $this->getConfig('authtoken'); $rooms = $this->getConfig('join'); // First, join the room if (!$rooms) { throw new Exception('Not configured to join any rooms!'); } $this->readBuffers = array(); // Set up our long poll in a curl multi request so we can // continue running while it executes in the background $this->multiHandle = curl_multi_init(); $this->readHandles = array(); foreach ($rooms as $room_id) { $this->joinRoom($room_id); // Set up the curl stream for reading $url = $this->buildStreamingUrl($room_id); $ch = $this->readHandles[$url] = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_USERPWD, $this->authtoken . ':x'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'read')); curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); curl_setopt($ch, CURLOPT_TIMEOUT, 0); curl_multi_add_handle($this->multiHandle, $ch); // Initialize read buffer $this->readBuffers[$url] = ''; } $this->active = null; $this->blockingMultiExec(); }
function add_url_to_multi_handle($mh, $url_list) { static $index = 0; // if we have another url to get if ($url_list[$index]) { // new curl handle $ch = curl_init(); // set the url curl_setopt($ch, CURLOPT_URL, $url_list[$index]); // to prevent the response from being outputted curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // follow redirections curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // do not need the body. this saves bandwidth and time curl_setopt($ch, CURLOPT_NOBODY, 1); // add it to the multi handle curl_multi_add_handle($mh, $ch); // increment so next url is used next time $index++; return true; } else { // we are done adding new URLs return false; } }
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"; }
public static function ad_counter(\ApiParam $params) { $ids = array_filter(explode(',', $params->adIds)); if (count($ids) > 100) { return 'limit 100'; } $main = curl_multi_init(); $handles = array(); foreach ($ids as $key => $id) { $url = "http://counter.baixing.net/c.php?id={$id}"; $handles[$key] = curl_init($url); curl_setopt($handles[$key], CURLOPT_URL, $url); curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1); curl_multi_add_handle($main, $handles[$key]); \Logger::eventLog('adcounter', (array) $params); } $running = 0; $i = 0; do { curl_multi_exec($main, $running); $i++; usleep(1000); } while ($running > 0 && $i < 1000); foreach ($ids as $key => $id) { //$results[] = curl_multi_getcontent($handles[$key]); curl_multi_remove_handle($main, $handles[$key]); } curl_multi_close($main); return 'ok'; }
/** * 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; }
/** * add a new request to the pool. */ public function add(Request $request, array $opts = array()) { $ch = $request->build($opts); $this->requests[(int) $request->resource] = $request; curl_multi_add_handle($this->resource, $request->resource); return $request; }
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 }
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); }
/** * {@inheritdoc} */ public function execute(callable $callback = null) { $stacks = $this->buildStacks(); foreach ($stacks as $requests) { // Tell each request to use this dispatcher. foreach ($requests as $request) { $status = curl_multi_add_handle($this->handle, $request->getHandle()); if ($status !== CURLM_OK) { throw new CurlErrorException(sprintf('Unable to add request to cURL multi handle (code #%u)', $status)); } } // Start dispatching the requests. $this->dispatch(); // Loop through all requests and remove their relationship to our dispatcher. foreach ($requests as $request) { if ($request->isSuccessful() === false) { throw new CurlErrorException($request->getErrorMessage()); } $request->setRawResponse(curl_multi_getcontent($request->getHandle())); curl_multi_remove_handle($this->handle, $request->getHandle()); if ($callback !== null) { $callback($request->getResponse()); } } } }
private function addHandle($mh, $url) { $ch = curl_init($url); $this->setOneOption($ch); curl_multi_add_handle($mh, $ch); return $ch; }
/** * Response constructor. */ public function __construct() { $this->curlMultiHandle = curl_multi_init(); $this->curlHandle = curl_init(); curl_multi_add_handle($this->curlMultiHandle, $this->curlHandle); $this->data = ''; }
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; }
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; }