首页 > 编程语言 >好用的PHP高性能多并发restful的HTTP Client

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

时间:2022-11-14 17:32:11浏览次数:59  
标签:HTTP Response Client timeout http array PHP response CURLOPT

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

  

 

作者:​​sunsky303​​


标签:HTTP,Response,Client,timeout,http,array,PHP,response,CURLOPT
From: https://blog.51cto.com/u_15715098/5849648

相关文章

  • 解决linux系统中对两个不同的网络连接http访问
    项目5G问题背景:调用网络端的服务器服务,对本地机器人进行控制,以实现机器人应执行的任务。需求:主机需要访问外网的API,同时在本地局域网与机器人的API进行通信。问题:要解决......
  • 好用的HTTP代理如何获得
    我们每个人在做爬虫业务时,肯定都想一下子找到好用的HTTP代理,用在业务上,既省时又省力,但是很多时候都事与愿违,市面上各种繁杂的代理套餐非常多,如何能从选出既适合自己业......
  • 用asp.net 实现断点续传 (HTTP)
    ​HTML部分 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="up6.index" %><!DOCTYPE html PUBLIC "-//W3C//DTDXHTM......
  • 踩坑JSTL标签库:/WEB-INF/jsp/fore/home.jsp (行.: [3], 列: [0]) 无法在web.xml或使用
    Tomcat9及之前使用到的JSTL库:https://files-cdn.cnblogs.com/files/zhangzhixi/jstl.zip最近心血来潮想复习一下JavaWEB,调试JSTL标签的时候出现了一些错误,如标题所示。先......
  • HTTP Error 500.31 - Failed to load ASP.NET Core runtime
    使用ASP.NETCoreDiagnosticsforIIS/IISExpressCannotlocateruntimeconfigfileHttpApi.Host\HttpApi.Host.runtimeconfig.json HTTPError500.31-Failedt......
  • 用jsp 实现断点续传 (HTTP)
    ​1、介绍enctypeenctype 属性规定发送到服务器之前应该如何对表单数据进行编码。enctype作用是告知服务器请求正文的MIME类型(请求消息头content-type的作用一样)1、1......
  • JMeter自定义HTTP组件
    JMeter是一个优秀的开源项目,我们可以在jmeter的官网了解到如何使用和如何二次开发:https://jmeter.apache.org/因工作需要,最近做了一个JMeter自定义的http组件(其实就是在ht......
  • 响应的HTTP协议
    1.响应行  (1)响应的协议和版本号  (2)响应状态码  (3)响应状态述符2.响应头   key:value 不同的响应头有不同的含义 空行3.响应体  回传给客户......
  • RestClient查询文档
    查询的基本步骤是:1.创建SearchRequest对象2.准备Request.source(),也就是DSL。1.QueryBuilders来构建查询条件2.传入Request.source()的query(......
  • PHP三元运算符 ?? 和 ?:
      $c=$a?:$b;等效于$c=$a?$a:$b;$c=$a??$b;等效于$c=isset($a)?$a:$b;示例:$a=null;$b='b';$c=$a?:$b;//b$c......