• 好用的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 MultiHttpRequest; use MultiHttpResponse; //单个请求 $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, " "; } ?> //Multi-request 多个请求: <?php use MultiHttpMultiRequest; $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, ' ', " "; } ?>
    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  . 

      

     

  • 相关阅读:
    [bzoj2259][Oibh]新型计算机_Dijkstra
    [bzoj1660][Usaco2006 Nov]Bad Hair Day_单调栈
    [bzoj3943][Usaco2015 Feb]SuperBull_Kruskal
    [bzoj2131]免费的馅饼_树状数组
    [bzoj3932][CQOI2015]任务查询系统_主席树
    软件图标大全网站
    提示用户一直输入数字(默认为正整数),当用户输入end的时候显示当前输入数字中的最大值。
    打印多边形的菱形(for的嵌套)
    while循环问题(老师询问问题,学生回答。学生会了可以放学,或者老师讲了10遍,还是没有会的,被迫无奈也要放学。)
    while练习:输入一个班级的人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩。
  • 原文地址:https://www.cnblogs.com/sunsky303/p/13364642.html
Copyright © 2020-2023  润新知