PHP code example of fastgoo / async-http-client

1. Go to this page and download the library: Download fastgoo/async-http-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fastgoo / async-http-client example snippets


# ----------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');