本文整理汇总了PHP中curl_multi_close函数的典型用法代码示例。如果您正苦于以下问题:PHP curl_multi_close函数的具体用法?PHP curl_multi_close怎么用?PHP curl_multi_close使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了curl_multi_close函数的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: __destruct
public function __destruct()
{
if (isset($this->_mh)) {
curl_multi_close($this->_mh);
unset($this->_mh);
}
}
示例4: execute
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;
}
示例5: 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";
}
示例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: __destruct
/**
* Destruct - conditionally closes all open curl handles.
* @return null
*/
public function __destruct()
{
curl_multi_close($this->mc_handle);
if ($this->close_curl_handles) {
$this->destroyCurlQueue();
}
}
示例8: 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;
}
示例9: main
function main()
{
$port = $_ENV['SERVERPORT'];
$host = php_uname('n');
$url = "http://{$host}:{$port}/hello.php";
$ch1 = curl_init();
$ch2 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_WRITEFUNCTION, 'except');
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_HEADER, 0);
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);
$active = null;
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);
}
}
curl_multi_close($mh);
}
示例10: sendMultiRequests
public static function sendMultiRequests($urls)
{
$result = "";
$chs = array();
$mh = curl_multi_init();
try {
foreach ($urls as $url) {
$chs[$url] = curl_init($url);
curl_setopt_array($chs[$url], self::$curl_options);
curl_multi_add_handle($mh, $chs[$url]);
}
$running = 0;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
} catch (Exception $e) {
print_r($e->getMessage());
} finally {
foreach ($chs as $url => $curl) {
$info = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($info == 200) {
$result = $url;
}
curl_multi_remove_handle($mh, $curl);
}
curl_multi_close($mh);
}
return $result;
}
示例11: reqMultiCurls
function reqMultiCurls($urls)
{
// for storing cUrl handlers
$chs = array();
// for storing the reponses strings
$contents = array();
// loop through an array of URLs to initiate
// one cUrl handler for each URL (request)
foreach ($urls as $url) {
$ch = curl_init($url);
// tell cUrl option to return the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$chs[] = $ch;
}
// initiate a multi handler
$mh = curl_multi_init();
// add all the single handler to a multi handler
foreach ($chs as $key => $ch) {
curl_multi_add_handle($mh, $ch);
}
// execute the multi cUrl handler
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM || $active);
// retrieve the reponse from each single handler
foreach ($chs as $key => $ch) {
if (curl_errno($ch) == CURLE_OK) {
$contents[] = curl_multi_getcontent($ch);
} else {
echo "Err>>> " . curl_error($ch) . "\n";
}
}
curl_multi_close($mh);
return $contents;
}
示例12: execute
public function execute()
{
$mh = curl_multi_init();
$chs = $this->chs();
$stillRunning = 0;
$result = array();
if (function_exists('curl_multi_setopt')) {
curl_multi_setopt($mh, CURLMOPT_PIPELINING, 1);
curl_multi_setopt($mh, CURLMOPT_MAXCONNECTS, $this->maxConnectsSize());
}
foreach ($chs as $ch) {
curl_multi_add_handle($mh, $ch);
}
do {
$execReturnValue = curl_multi_exec($mh, $stillRunning);
curl_multi_select($mh);
} while ($stillRunning > 0);
foreach ($chs as $i => $ch) {
$curlError = curl_error($ch);
if ($curlError === "") {
$result[$i] = curl_multi_getcontent($ch);
} else {
throw new PubnubException("Curl error on handle {$i}: {$curlError}\n");
}
curl_multi_remove_handle($mh, $ch);
curl_close($ch);
}
curl_multi_close($mh);
$this->requests = array();
if ($execReturnValue != CURLM_OK) {
throw new PubnubException(curl_multi_strerror($execReturnValue));
}
return $result;
}
示例13: makeRequests
/**
* @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;
}
示例14: __destruct
public function __destruct()
{
// Destroy the multi handle.
if (!empty($this->multi)) {
curl_multi_close($this->multi);
}
}
示例15: multi_request
function multi_request($urls)
{
$curly = array();
$result = array();
$mh = curl_multi_init();
foreach ($urls as $id => $url) {
$curly[$id] = curl_init();
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curly[$id], CURLOPT_TIMEOUT, 30);
curl_setopt($curly[$id], CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curly[$id], CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curly[$id], CURLOPT_SSL_VERIFYHOST, 0);
curl_multi_add_handle($mh, $curly[$id]);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
foreach ($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
curl_multi_close($mh);
return $result;
}