本文整理汇总了PHP中curl_multi_init函数的典型用法代码示例。如果您正苦于以下问题:PHP curl_multi_init函数的具体用法?PHP curl_multi_init怎么用?PHP curl_multi_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了curl_multi_init函数的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: 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
}
示例4: __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 = '';
}
示例5: 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;
}
示例6: 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;
}
示例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: 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;
}
示例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: _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;
}
示例11: __construct
/**
* Constructor is private final. Can only be instantiated via WebRequest::Instance()
* @access private
* @final
*/
private final function __construct($config)
{
$this->mh = curl_multi_init();
$this->config = $config;
if (isset($this->config['endpoint_timeout'])) {
$this->_options[CURLOPT_TIMEOUT] = $this->config['endpoint_timeout'];
}
$this->_options[CURLOPT_USERAGENT] = 'Instaphp/' . $this->config['version'];
if (isset($this->config['no_verify_peer'])) {
$this->_options[CURLOPT_SSL_VERIFYPEER] = false;
}
//-- this is an interesting hack to make curl+ssl+windows follow redirects
//-- without skipping verification. For some reason, the version of libcurl/curl
//-- included with ZendServer CE doesn't use the systems CA bundle, so, we specify
//-- the path to the cert here (via config setting)
if (isset($this->config['ca_cert_bundle_path']) && !empty($this->config['ca_cert_bundle_path'])) {
$this->_options[CURLOPT_SSL_VERIFYPEER] = true;
$this->_options[CURLOPT_SSL_VERIFYHOST] = 2;
$this->_options[CURLOPT_SSLVERSION] = 3;
$this->_options[CURLOPT_CAINFO] = $this->config['ca_cert_bundle_path'];
}
$this->_options[CURLOPT_HTTPHEADER] = array("Connection: keep-alive", "Keep-Alive: 300", "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7", "Accept-Language: en-us,en;q=0.5");
$this->_requests = array();
$this->_responses = array();
}
示例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: curlInit
/**
* 初始化(在运行阶段多次并发,每次并发前都需要初始化)
*/
public function curlInit()
{
$ResourceType = get_resource_type($this->_curlMultiObj);
if ($ResourceType !== 'curl') {
$this->_curlMultiObj = curl_multi_init();
}
}
示例14: 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;
}
示例15: 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);
}