本文整理汇总了PHP中curl_multi_add_handle函数的典型用法代码示例。如果您正苦于以下问题:PHP curl_multi_add_handle函数的具体用法?PHP curl_multi_add_handle怎么用?PHP curl_multi_add_handle使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了curl_multi_add_handle函数的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: add_handle
/**
* 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);
}
示例4: execute
/**
* {@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());
}
}
}
}
示例5: __construct
/**
* Response constructor.
*/
public function __construct()
{
$this->curlMultiHandle = curl_multi_init();
$this->curlHandle = curl_init();
curl_multi_add_handle($this->curlMultiHandle, $this->curlHandle);
$this->data = '';
}
示例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: checkForClosedFilePointer
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";
}
示例8: add_url_to_multi_handle
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;
}
}
示例9: curlMultiRequest
/**
* 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;
}
示例10: multiGet
/**
* @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;
}
示例11: _multi_curl
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;
}
示例12: 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);
}
示例13: batch
/**
* @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;
}
示例14: curlMultiAddHandle
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 ресурсом
}
示例15: multi_get_urls
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;
}