好用的PHP高性能多并发restful的HTTP Client


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,否则超时不支持小数。

 

 

 

  大家好,今天的主角是它: https://github.com/sinacms/MultiHttp ,这是本人写的一个curl工具库,在生产中十分好用,所以拿出来分享给大家,欢迎大家提issue/merge 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 多个请求: <?php use 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"; } ?>

 

options选项有:

   '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 ,

  

    怎么样,什么人性、直观吧,

  另外,建议大家用最新stable版本, 有很多好用的feature, 比如 expectsJson() 会直接验证response是json,并解析成php array( or hashmap),更多特性请看tests目录的使用。

  欢迎大家使用它  https://github.com/sinacms/MultiHttp  . 

  

 

posted @ 2020-07-23 10:02  sunsky303  阅读(909)  评论(0编辑  收藏  举报
显示搜索