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 #2
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 #3
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;
 }
Example #4
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 #5
0
function chk($proxies)
{
	$mc = curl_multi_init ();
	for ($thread_no = 0; $thread_no<count ($proxies); $thread_no++)
	{
		$c [$thread_no] = curl_init ();
		curl_setopt ($c [$thread_no], CURLOPT_URL, "http://google.com");
		curl_setopt ($c [$thread_no], CURLOPT_HEADER, 0);
		curl_setopt ($c [$thread_no], CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($c [$thread_no], CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt ($c [$thread_no], CURLOPT_TIMEOUT, 10);
		curl_setopt ($c [$thread_no], CURLOPT_PROXY, trim ($proxies [$thread_no]));
		curl_setopt ($c [$thread_no], CURLOPT_PROXYTYPE, 0);
		curl_multi_add_handle ($mc, $c [$thread_no]);
	}
	do 
	{
		while (($execrun = curl_multi_exec ($mc, $running)) == CURLM_CALL_MULTI_PERFORM)
		if ($execrun != CURLM_OK) break;
		while ($done = curl_multi_info_read ($mc))
		{
			$info = curl_getinfo ($done ['handle']);
			if ($info ['http_code'] == 301) 
				echo trim ($proxies [array_search ($done['handle'], $c)])."<br>\r\n";
			curl_multi_remove_handle ($mc, $done ['handle']);
		}
	} while ($running);
	curl_multi_close ($mc);
}
 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);
     }
 }
 /**
  * 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 #8
0
 function _lookForCompleted($execute = true)
 {
     // Get information about the handle that just finished the work.
     while ($done = curl_multi_info_read($this->mh)) {
         // Call the associated listener
         foreach ($this->transfers as $lk => $listener) {
             // Strict compare handles.
             if ($listener->getCurl() === $done['handle']) {
                 $error = null;
                 if ($done['result'] != CURLE_OK) {
                     $error = curl_error($done['handle']);
                 }
                 $listener->Call($error);
                 curl_multi_remove_handle($this->mh, $done['handle']);
                 if ($execute) {
                     curl_close($done['handle']);
                     if ($this->_done) {
                         call_user_func($this->_done, $listener->getId());
                     }
                 } else {
                     $this->queue[] = $listener;
                 }
                 unset($this->transfers[$lk]);
             }
         }
     }
 }
Example #9
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);
 }
Example #10
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;
 }
Example #11
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);
 }
Example #12
0
 public function run()
 {
     do {
         $status = curl_multi_exec($this->multicurl, $active);
         $info = curl_multi_info_read($this->multicurl);
         if (false !== $info) {
             $handle = $info['handle'];
             $index = array_search($handle, $this->connections);
             $callback = $this->callbacks[$index];
             // If the handle has successfully fetched content
             if ($info['msg'] == 1 && $info['result'] == 0) {
                 if ($callback != null) {
                     $result = 0;
                     $content = curl_multi_getcontent($handle);
                     call_user_func($callback, $result, $content);
                 }
             } else {
                 if ($info["msg"] == 1) {
                     if ($callback != null) {
                         $result = $info['result'];
                         $content = '';
                         call_user_func($callback, $result, $content);
                     }
                 }
             }
         }
     } while ($status === CURLM_CALL_MULTI_PERFORM || $active);
 }
Example #13
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 #14
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 #15
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);
     }
 }
Example #16
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
 }
Example #17
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 #18
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));
             }
         }
     }
 }
Example #19
0
 private function store()
 {
     while ($done = curl_multi_info_read($this->hdl)) {
         $key = (string) $done['handle'];
         $this->responses[$key] = curl_multi_getcontent($done['handle']);
         curl_multi_remove_handle($this->hdl, $done['handle']);
     }
 }
Example #20
0
 public function send(array $messages)
 {
     if (empty($messages)) {
         return array();
     }
     $queue = curl_multi_init();
     $map = array();
     foreach ($messages as $key => $message) {
         $ch = curl_init();
         $mobiles = implode(',', $message->getMobiles());
         $u = $this->url . "?sn={$this->sn}&pwd={$this->pwd}&mobile={$mobiles}&content={$message->getContent()}&ext=&stime=&rrid=";
         curl_setopt($ch, CURLOPT_URL, $u);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_multi_add_handle($queue, $ch);
         $map[(string) $ch] = $key;
         //            $map[$key] = $ch;
     }
     $active = null;
     $responses = array();
     do {
         while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) {
         }
         if ($code != CURLM_OK) {
             break;
         }
         // a request was just completed -- find out which one
         while ($done = curl_multi_info_read($queue)) {
             // get the info and content returned on the request
             $info = curl_getinfo($done['handle']);
             $error = curl_error($done['handle']);
             $results = curl_multi_getcontent($done['handle']);
             $responses[$map[(string) $done['handle']]] = compact('info', 'error', 'results');
             // remove the curl handle that just completed
             curl_multi_remove_handle($queue, $done['handle']);
             curl_close($done['handle']);
         }
         // Block for data in / output; error handling is done by curl_multi_exec
         if ($active > 0) {
             curl_multi_select($queue, 0.5);
         }
     } while ($active);
     curl_multi_close($queue);
     $errors = array();
     foreach ($responses as $key => $response) {
         if (!$response) {
             $errors[] = $messages[$key];
         } else {
             $dom = \DOMDocument::loadXML($response['results']);
             $elements = $dom->getElementsByTagName("string");
             $element = $elements->item(0);
             if (substr($element->nodeValue, 0, 1) == '-') {
                 $errors[] = $messages[$key];
             }
         }
     }
     return $errors;
 }
Example #21
0
 /**
  * Poll completed cURL entries.
  * @return array
  */
 protected function readCompletedEntries()
 {
     $entries = [];
     // DO NOT call curl_multi_add_handle() until polling done
     while ($entry = curl_multi_info_read($this->mh)) {
         $entries[] = $entry;
     }
     return $entries;
 }
Example #22
0
 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->beforeSend($this->before_send_function);
             $curl->success($this->success_function);
             $curl->error($this->error_function);
             $curl->complete($this->complete_function);
             $curl->base_url = $url;
             $curl->url = $this->buildURL($url, $data);
             $curl->setOpt(CURLOPT_URL, $curl->url, $curl->curl);
             $curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
             $curl->setOpt(CURLOPT_HTTPGET, true);
             $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 {
             curl_multi_select($curl_multi);
             $status = curl_multi_exec($curl_multi, $active);
         } while ($status === CURLM_CALL_MULTI_PERFORM || $active);
         while (!($info_array = curl_multi_info_read($curl_multi)) === false) {
             if (!($info_array['msg'] === CURLMSG_DONE)) {
                 continue;
             }
             foreach ($this->curls as $ch) {
                 if ($ch->curl === $info_array['handle']) {
                     $ch->curl_error_code = $info_array['result'];
                     break;
                 }
             }
         }
         foreach ($this->curls as $ch) {
             $this->exec($ch);
         }
     } else {
         $this->base_url = $url_mixed;
         $this->url = $this->buildURL($url_mixed, $data);
         $this->setOpt(CURLOPT_URL, $this->url);
         $this->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
         $this->setOpt(CURLOPT_HTTPGET, true);
         return $this->exec();
     }
 }
Example #23
0
 private function storeResponses()
 {
     while ($done = curl_multi_info_read($this->mc)) {
         $key = (string) $done['handle'];
         $this->responses[$key]['data'] = curl_multi_getcontent($done['handle']);
         foreach ($this->properties as $name => $const) {
             $this->responses[$key][$name] = curl_getinfo($done['handle'], $const);
             curl_multi_remove_handle($this->mc, $done['handle']);
         }
     }
 }
Example #24
0
 public function execute()
 {
     $this->running = true;
     if (!is_resource(self::$multiHandle)) {
         self::$multiHandle = curl_multi_init();
     }
     foreach ($this->requests as $request) {
         $this->addHandle($request);
     }
     $finish = false;
     $running = true;
     do {
         $finish = !$running;
         while (CURLM_CALL_MULTI_PERFORM == ($execrun = curl_multi_exec(self::$multiHandle, $running))) {
         }
         if ($execrun != CURLM_OK) {
             break;
         }
         while ($done = curl_multi_info_read(self::$multiHandle)) {
             $ch = $done['handle'];
             $info = curl_getinfo($ch);
             $url = self::getHeader($info['request_header'], 'X-Url');
             $request = $this->requests[$url];
             $rawResponse = curl_multi_getcontent($ch);
             if (preg_match("@^HTTP/\\d\\.\\d 200 Connection established\r\n\r\n@i", $rawResponse)) {
                 list(, $header, $body) = explode("\r\n\r\n", $rawResponse, 3);
             } else {
                 list($header, $body) = explode("\r\n\r\n", $rawResponse, 2);
             }
             $response = new CurlResponse();
             $response->request = $request;
             $response->status = $info['http_code'];
             $headerNames = array('etag' => 'ETag', 'contentType' => 'Content-Type', 'link' => 'Link');
             foreach ($headerNames as $key => $name) {
                 $response->{$key} = Curl::getHeader($header, $name);
             }
             if (200 == $response->status) {
                 $response->content = $body;
             }
             $callback = $request->callback;
             $callback($response);
             curl_close($ch);
             curl_multi_remove_handle(self::$multiHandle, $ch);
         }
         if ($running || !$finish) {
             if (curl_multi_select(self::$multiHandle, 1) === -1) {
                 usleep(250);
             }
         }
     } while ($running || !$finish);
     $this->running = false;
     return true;
 }
Example #25
0
 /**
  * [getMultiUser 多进程获取用户数据]
  * @param  [type] $user_list [description]
  * @return [type]            [description]
  */
 public static function getMultiUser($user_list)
 {
     $ch_arr = array();
     $text = array();
     $len = count($user_list);
     $max_size = $len > 5 ? 5 : $len;
     $requestMap = array();
     $mh = curl_multi_init();
     for ($i = 0; $i < $max_size; $i++) {
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_URL, 'http://www.zhihu.com/people/' . $user_list[$i] . '/about');
         curl_setopt($ch, CURLOPT_COOKIE, self::$user_cookie);
         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36');
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
         $requestMap[$i] = $ch;
         curl_multi_add_handle($mh, $ch);
     }
     $user_arr = array();
     do {
         while (($cme = curl_multi_exec($mh, $active)) == CURLM_CALL_MULTI_PERFORM) {
         }
         if ($cme != CURLM_OK) {
             break;
         }
         while ($done = curl_multi_info_read($mh)) {
             $info = curl_getinfo($done['handle']);
             $tmp_result = curl_multi_getcontent($done['handle']);
             $error = curl_error($done['handle']);
             $user_arr[] = array_values(getUserInfo($tmp_result));
             //保证同时有$max_size个请求在处理
             if ($i < sizeof($user_list) && isset($user_list[$i]) && $i < count($user_list)) {
                 $ch = curl_init();
                 curl_setopt($ch, CURLOPT_HEADER, 0);
                 curl_setopt($ch, CURLOPT_URL, 'http://www.zhihu.com/people/' . $user_list[$i] . '/about');
                 curl_setopt($ch, CURLOPT_COOKIE, self::$user_cookie);
                 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36');
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                 $requestMap[$i] = $ch;
                 curl_multi_add_handle($mh, $ch);
                 $i++;
             }
             curl_multi_remove_handle($mh, $done['handle']);
         }
         if ($active) {
             curl_multi_select($mh, 10);
         }
     } while ($active);
     curl_multi_close($mh);
     return $user_arr;
 }
Example #26
0
 public function proceed()
 {
     if (!$this->queue) {
         return;
     }
     if (!$this->curlm && false === ($this->curlm = curl_multi_init())) {
         throw new ClientException('Unable to create a new cURL multi handle');
     }
     foreach (array_keys($this->queue) as $i) {
         if (3 == count($this->queue[$i])) {
             // prepare curl handle
             list($request, , $options) = $this->queue[$i];
             $curl = static::createCurlHandle();
             // remove custom option
             unset($options['callback']);
             $this->prepare($curl, $request, $options);
             $this->queue[$i][] = $curl;
             curl_multi_add_handle($this->curlm, $curl);
         }
     }
     // process outstanding perform
     $active = null;
     do {
         $mrc = curl_multi_exec($this->curlm, $active);
     } while ($active && CURLM_CALL_MULTI_PERFORM == $mrc);
     // handle any completed requests
     while ($done = curl_multi_info_read($this->curlm)) {
         foreach (array_keys($this->queue) as $i) {
             list($request, $response, $options, $curl) = $this->queue[$i];
             if ($curl !== $done['handle']) {
                 continue;
             }
             // populate the response object
             if (CURLE_OK === $done['result']) {
                 static::populateResponse($curl, curl_multi_getcontent($curl), $response);
             }
             // remove from queue
             curl_multi_remove_handle($this->curlm, $curl);
             static::unsetWipHeader($curl);
             curl_close($curl);
             unset($this->queue[$i]);
             // callback
             if (isset($options['callback'])) {
                 call_user_func($options['callback'], $this, $request, $response, $options, $done['result']);
             }
         }
     }
     // cleanup
     if (!$this->queue) {
         curl_multi_close($this->curlm);
         $this->curlm = null;
     }
 }
Example #27
0
 public function streamIsActive()
 {
     curl_multi_select($this->curlMultiHandle);
     $active = null;
     do {
         $mrc = curl_multi_exec($this->curlMultiHandle, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     if ($active) {
         return true;
     }
     $info = curl_multi_info_read($this->curlMultiHandle);
     return is_array($info);
 }
Example #28
0
 public function rolling_curl()
 {
     //echo json_encode($this->params);exit();
     $queue = curl_multi_init();
     $map = array();
     foreach ($this->params as $k => $param) {
         if (!$this->verify_url($param['url'])) {
             continue;
         }
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $param['url']);
         curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_NOSIGNAL, true);
         $post_params = $this->verify_params($param['params']);
         $this->build_query($post_params);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->query_post);
         curl_multi_add_handle($queue, $ch);
         $map[(string) $ch] = $k;
     }
     $responses = array();
     do {
         while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) {
         }
         if ($code != CURLM_OK) {
             break;
         }
         // a request was just completed -- find out which one
         while ($done = curl_multi_info_read($queue)) {
             //return $map;
             //return $done;
             // get the info and content returned on the request
             //$info = curl_getinfo($done['handle']);
             //$error = curl_error($done['handle']);
             //$results = $this -> callback(curl_multi_getcontent($done['handle']), $this -> delay);
             //$responses[$map[(string)$done['handle']]] = compact('info', 'error', 'results');
             $results = json_decode(curl_multi_getcontent($done['handle']), 1);
             $responses[$map[(string) $done['handle']]] = $results;
             // remove the curl handle that just completed
             curl_multi_remove_handle($queue, $done['handle']);
             curl_close($done['handle']);
         }
         // Block for data in / output; error handling is done by curl_multi_exec
         if ($active > 0) {
             curl_multi_select($queue, 0.5);
         }
     } while ($active);
     curl_multi_close($queue);
     return $responses;
 }
Example #29
0
 /**
  * @return \Generator
  */
 public function run()
 {
     $running = 0;
     do {
         while (($ret = curl_multi_exec($this->curl, $running)) === CURLM_CALL_MULTI_PERFORM) {
         }
         if ($ret !== CURLM_OK) {
             break;
         }
         while (false !== ($done = curl_multi_info_read($this->curl))) {
             $response = $this->createResponse($done);
             $this->traceResponse($response);
             (yield $response);
         }
     } while ($running);
 }
function rolling_curl($urls, $delay)
{
    $queue = curl_multi_init();
    $map = array();
    foreach ($urls as $url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_NOSIGNAL, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 500);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPGET, true);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        curl_multi_add_handle($queue, $ch);
        $map[(string) $ch] = $url;
    }
    $responses = array();
    do {
        while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) {
        }
        if ($code != CURLM_OK) {
            break;
        }
        // a request was just completed -- find out which one
        while ($done = curl_multi_info_read($queue)) {
            // get the info and content returned on the request
            $info = curl_getinfo($done['handle']);
            $error = curl_error($done['handle']);
            $results = curl_multi_getcontent($done['handle']);
            $responses[$map[(string) $done['handle']]] = compact('info', 'error', 'results');
            // remove the curl handle that just completed
            curl_multi_remove_handle($queue, $done['handle']);
            curl_close($done['handle']);
        }
        // Block for data in / output; error handling is done by curl_multi_exec
        if ($active > 0) {
            curl_multi_select($queue, 0.5);
        }
    } while ($active);
    curl_multi_close($queue);
    return $responses;
}