好用的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  . 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM