GithubHelp home page GithubHelp logo

asynchttpclient's Introduction

基于Swoole扩展的HTTP异步客户端

Latest Version Php Version Swoole Version

简介

基于 Swoole 异步客户端的扩展包,可以像使用 GuzzleHttp 简单优雅的使用swoole的异步客户端,无需关注底层实现以及处理逻辑。可实现同时发起N个HTTP请求不会被阻塞。经测试循环并发100个请求,全部返回结果只需要3-4秒的时间。

  • 基于 Swoole 扩展
  • 需在CLI模式下运行
  • 支持HTTPS 与 HTTP 2种协议
  • 使用 HTTPS 必须在编译swoole时启用--enable-openssl
  • 解决高并发请求(可做接口压测)
  • 异步 HTTP 请求,非阻塞
  • 该扩展适用于目前所有现代框架
  • 作者qq 773729704、微信 huoniaojugege  加好友备注github

更新记录

  • 2018.03.30 去掉get请求的参数,参数让用户自己带在URL上

参考文档

中文文档

环境要求

  1. PHP 7.0 +
  2. Swoole 1.9 +
  3. Composer

Composer 安装

  • composer require fastgoo/async-http-client

使用范例

# ----------HTTP 请求(请求方式 get post put delete)--------
$client = new \AsyncClient\Client("https://open.fastgoo.net");
$params = [
    'address' => '邮箱',
    'subject' => '标题',
    'body' => '内容',
];

# 单请求
$client->post('/base.api/email/send', $params, function (\Swoole\Http\Client $client) {
    var_dump($client->body);
});

# 多请求
foreach (range(1, 50) as $v) {
    $client->post('/base.api/email/send', $params, function (\Swoole\Http\Client $client) {
        var_dump($client->body);
    });
}

# 发送请求文件
$client->setFiles(['files' => __DIR__ . '/fastgoo-logo.png'])->post('/base.api/file/upload',[],function (\Swoole\Http\Client $client) {
    var_dump($client->body);
})

# 开始发送请求
$client->send();
# -----------------------------END--------------------------



# 下载文件范例
$client = new \AsyncClient\Client("https://timgsa.baidu.com");
$client->download('/timg?image&quality=80&size=b9999_10000&sec=1521617943&di=913c0898b55cf2992d6d5136013e98d2&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.taopic.com%2Fuploads%2Fallimg%2F120727%2F201995-120HG1030762.jpg', './logoaa.png', function (\Swoole\Http\Client $client) {
    var_dump($client);
});
$client->send();


# 静态对象实例化请求
\AsyncClient\Client::init("https://open.fastgoo.net")->post('/base.api/email/send', [], function (\Swoole\Http\Client $client) {
    var_dump($client->body);
})->send();

# 请求设置Cookies 和 Headers
$client->setCookies([]);
$client->setHeaders([]);

# 重点强调,由于默认不设置ip会自动异步解析DNS取到IP然后才能创建客户端,
# 异步解析DNS需要在CLI的模式下才能使用
# 如果条件允许的情况下尽量存取IP然后去发起请求,不然调外部网络会造成请求阻塞,性能优势会有所下降
$client->setDnsIp('192.168.1.1');

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.