/*****************************************************
CURL 非阻塞調(diào)用類
Auther: Linvo
Copyright(C) 2010/10/21
*******************************************************/
/*
// 使用范例
// 傳入?yún)?shù)說明
// url 請求地址
// data POST方式數(shù)據(jù)
//并發(fā)調(diào)用
$param1 = array(
array(
'url' => "http://localhost/a.php?s=1",
),
array(
'url' => "http://localhost/a.php?s=1",
'data' => array('aaa' => 1, 'bbb' => 2),
),
);
//單個調(diào)用
$param2 = array(
'url' => "http://localhost/a.php?s=0",
'data' => array('aaa' => 1, 'bbb' => 2),
);
//單個調(diào)用(GET簡便方式)
$param3 = 'http://localhost/a.php?s=2';
$ac = new AsyncCURL();
$ac->set_param($param1);
$ret = $ac->send();
//返回值為請求參數(shù)順序的結(jié)果數(shù)組(元素值為False表示請求錯誤)
var_dump($ret);
*/
class AsyncCURL
{
/**
* 是否需要返回HTTP頭信息
*/
public $curlopt_header = 0;
/**
* 單個CURL調(diào)用超時限制
*/
public $curlopt_timeout = 20;
private $param = array();
/**
* 構(gòu)造函數(shù)(可直接傳入請求參數(shù))
*
* @param array 可選
* @return void
*/
public function __construct($param = False)
{
if ($param !== False)
{
$this->param = $this->init_param($param);
}
}
/**
* 設(shè)置請求參數(shù)
*
* @param array
* @return void
*/
public function set_param($param)
{
$this->param = $this->init_param($param);
}
/**
* 發(fā)送請求
*
* @return array
*/
public function send()
{
if(!is_array($this->param) || !count($this->param))
{
return False;
}
$curl = $ret = array();
$handle = curl_multi_init();
foreach ($this->param as $k => $v)
{
$param = $this->check_param($v);
if (!$param) $curl[$k] = False;
else $curl[$k] = $this->add_handle($handle, $param);
}
$this->exec_handle($handle);
foreach ($this->param as $k => $v)
{
if ($curl[$k])
{
$ret[$k] = curl_multi_getcontent($curl[$k]);
curl_multi_remove_handle($handle, $curl[$k]);
} else {
$ret[$k] = False;
}
}
curl_multi_close($handle);
return $ret;
}
//以下為私有方法
private function init_param($param)
{
$ret = False;
if (isset($param['url']))
{
$ret = array($param);
} else {
$ret = isset($param[0]) ? $param : False;
}
return $ret;
}
private function check_param($param = array())
{
$ret = array();
if (is_string($param))
{
$url = $param;
} else {
extract($param);
}
if (isset($url))
{
$url = trim($url);
$url = stripos($url, 'http://') === 0 ? $url : NULL;
}
if (isset($data) is_array($data) !empty($data))
{
$method = 'POST';
} else {
$method = 'GET';
unset($data);
}
if (isset($url)) $ret['url'] = $url;
if (isset($method)) $ret['method'] = $method;
if (isset($data)) $ret['data'] = $data;
$ret = isset($url) ? $ret : False;
return $ret;
}
private function add_handle($handle, $param)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $param['url']);
curl_setopt($curl, CURLOPT_HEADER, $this->curlopt_header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curlopt_timeout);
if ($param['method'] == 'POST')
{
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $param['data']);
}
curl_multi_add_handle($handle, $curl);
return $curl;
}
private function exec_handle($handle)
{
$flag = null;
do {
curl_multi_exec($handle, $flag);
} while ($flag > 0);
}
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《PHP中json格式數(shù)據(jù)操作技巧匯總》