微信号:phpgod

介绍:本公众号对PHP开发技术进行全面透析,所含内容适合各个阶段的PHP developer阅读和收藏,既然关注了就一定会有收获.

MultiHttp:高性能的 PHP 封装的 HTTP Restful 多线程

2017-10-06 09:00 Github

若文章排版不清或需要查看文中链接,请点击文章左下角的“阅读原文

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.

composer require sinacms/multihttp

Usage

Single-request:

<?php// Include Composer's autoload file if not already included.require __DIR__.'/vendor/autoload.php';use MultiHttp\Request;use MultiHttp\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 ($responses as $response) {  echo $response->request->uri, ' takes:', $response->duration,  "\n\t\n\t";
}?>

Multi-request:

<?phpuse MultiHttp\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 ($rtn as $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 ,
  • public static function create()

  • public function endCallback()

  • public function hasEndCallback()

  • public function onEnd(callable$callback)

  • public function uri

  • public function getIni($field)

  • public function addQuery($data)

  • public function post($uri, array $payload = array(), array $options = array())

  • public function addOptions(array $options = array())

  • public function get($uri, array $options = array())

  • public function send()

  • public function applyOptions()

  • public function makeResponse($isMultiCurl = false)

  • MultiRequest

  • public static function create()

  • public function addOptions(array $URLOptions)

  • public function add($method, $uri, array $payload = array(), array $options = array())

  • public function import(Request $request)

  • public function sendAll()

[More] https://github.com/sinacms/MultiHttp/blob/master/usage.md

关注微信公众号:PHP技术大全

PHPer升级为大神并不难!

深度技术交流群:374155314

我们聊聊技术+妹纸+人生。


 
PHP技术大全 更多文章 开源项目|TiddlyWiki 打造轻便个人 Wiki 知识库 宝塔Linux面板 – 9月20日更新 – 5.2.0正式版 PHPer之Laravel常见面试题及答案 Swoft-基于swoole2.0的高性能微服务框架 干货 | 一个MySQL 5.7分区表性能下降的案例分析
猜您喜欢 spring bean的作用域 【大宝推荐】创意的秘密——十四堂课带你体悟创意人生 [一周阅读清单] RxJava、Gradle、动态化、小工具推荐 C#获取当前程序运行路径的方法集合