MultiHttp
This is high performance curl wrapper written in pure PHP. It's compatible with PHP 5.4+ and HHVM. Notice that libcurl version must be over 7.36.0, otherwise timeout can not suppert decimal.
这是一个高性能的PHP封装的HTTP Restful多线程并发请求库,参考借鉴了httpresful 、multirequest等优秀的代码。它与PHP 5.4和hhvm兼容。 注意,libcurl版本必须>=7.36.0,否则超时不支持小数。
Contents
Feature
Installation
Usage
Single-request
Multi-request
Documentation
Request
MultiRequest
Feature
alias of curl option, e.g. 'timeout' equals 'CURLOPT_TIMEOUT' etc.
Request and MultiRequest class , can be used in any combination
graceful and efficient
Installation
You can use composer to install this library from the command line.
composerrequiresinacms/multihttp
Usage
Single-request:
// Include Composer's autoload file if not already included.
require__DIR__.'/vendor/autoload.php';
useMultiHttp\Request;
useMultiHttp\Response;
$responses=array();
$responses[]=Request::create()->addQuery('wd=good')->get('http://baidu.com?',array(
'timeout'=>3,
'timeout_ms'=>2000,
'callback'=>function(Response$response){
}))->send();
$responses[]=Request::create()->get('http://qq.com',array(
'callback'=>function(Response$response){
//sth
}))->addOptions(array(
'method'=>Request::PATCH,
'timeout'=>3,
))->send();
//test post
$responses[]=Request::create()->post(
'http://127.0.0.1',array('data'=>'this_is_post_data'),array(
'callback'=>function(Response$response){
//sth
}))->send();
foreach($responsesas$response){
echo $response->request->uri,' takes:',$response->duration,"\n\t\n\t";
}
?>
Multi-request:
useMultiHttp\MultiRequest;
$mr=MultiRequest::create();
$rtn=$mr->addOptions(
array(
array(
'url'=>'http://google.com',
'timeout'=>2,
'method'=>'HEAD',
'data'=>array(
),
'callback'=>function(Response$response){
//sth
}
),
))
->add('GET','http://sina.cn',array(),array(
'timeout'=>3
))
->import(Request::create()->trace('http://sohu.cn',array(
'timeout'=>3,
'callback'=>function(Response$response){
//sth
}))->applyOptions())
->send();
foreach($rtnas$response){
echo $response->request->uri,' takes:',$response->duration,' ',"\n\t\n\t";
}
?>
Documentation
Request
option shorthand
'url'=>'CURLOPT_URL',
'debug'=>'CURLOPT_VERBOSE',//for debug verbose
'method'=>'CURLOPT_CUSTOMREQUEST',
'data'=>'CURLOPT_POSTFIELDS',// array or string , file begin with '@'
'ua'=>'CURLOPT_USERAGENT',
'timeout'=>'CURLOPT_TIMEOUT',// (secs) 0 means indefinitely
'connect_timeout'=>'CURLOPT_CONNECTTIMEOUT',
'referer'=>'CURLOPT_REFERER',
'binary'=>'CURLOPT_BINARYTRANSFER',
'port'=>'CURLOPT_PORT',
'header'=>'CURLOPT_HEADER',// TRUE:include header
'headers'=>'CURLOPT_HTTPHEADER',// array
'download'=>'CURLOPT_FILE',// writing file stream (using fopen()), default is STDOUT
'upload'=>'CURLOPT_INFILE',// reading file stream
'transfer'=>'CURLOPT_RETURNTRANSFER',// TRUE:return string; FALSE:output directly (curl_exec)
'follow_location'=>'CURLOPT_FOLLOWLOCATION',
'timeout_ms'=>'CURLOPT_TIMEOUT_MS',// milliseconds, libcurl version > 7.36.0 ,
publicstaticfunctioncreate()
publicfunctionendCallback()
publicfunctionhasEndCallback()
publicfunctiononEnd(callable$callback)
publicfunctionuri
publicfunctiongetIni($field)
publicfunctionaddQuery($data)
publicfunctionpost($uri,array $payload=array(),array $options=array())
publicfunctionaddOptions(array $options=array())
publicfunctionget($uri,array $options=array())
publicfunctionsend()
publicfunctionapplyOptions()
publicfunctionmakeResponse($isMultiCurl=false)
MultiRequest
publicstaticfunctioncreate()
publicfunctionaddOptions(array $URLOptions)
publicfunctionadd($method,$uri,array $payload=array(),array $options=array())
publicfunctionimport(Request$request)
publicfunctionsendAll()
[More] https://github.com/sinacms/MultiHttp/blob/master/usage.md
更多分享,敬请关注
精彩不落幕Q群:374155314
本文来源网络,侵立删!