本文整理汇总了PHP中curl_multi_exec函数的典型用法代码示例。如果您正苦于以下问题:PHP curl_multi_exec函数的具体用法?PHP curl_multi_exec怎么用?PHP curl_multi_exec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了curl_multi_exec函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getMultiContents
/**
* 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;
}
示例2: exec
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;
}
示例3: batch
/**
* 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;
}
示例4: Run
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
}
示例5: getResult
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;
}
示例6: multiFetchRequest
/**
* 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;
}
示例7: subExec
/**
* 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);
}
示例8: get_multi_tags
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;
}
示例9: multiCurl
/**
* 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);
}
示例10: flush
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);
}
示例11: getResult
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;
}
示例12: executeMultiRequest
/**
* 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;
}
示例13: consumeStream
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);
}
示例14: run
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;
}
示例15: ping
/**
* 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;
}