php restful 多层,高性能的 PHP 封装的 HTTP Restful 多线程并发请求库-MultiHttp

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

1460000012983313?w=600&h=281

更多分享,敬请关注

精彩不落幕Q群:374155314

本文来源网络,侵立删!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值