PHP code example of pfinalclub / asyncio-http

1. Go to this page and download the library: Download pfinalclub/asyncio-http 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/ */

    

pfinalclub / asyncio-http example snippets




finalclub\AsyncioHttp\Client\Http;

// GET 请求示例
$response = Http::get('https://api.example.com/users')
    ->header('Authorization', 'Bearer token')
    ->timeout(5.0)
    ->send();

// POST 请求示例
$response = Http::post('https://api.example.com/users', [
    'name' => 'John',
    'email' => '[email protected]'
])
    ->header('Content-Type', 'application/json')
    ->send();

// 处理响应
if ($response->statusCode >= 200 && $response->statusCode < 300) {
    echo "请求成功: " . $response->body;
} else {
    echo "请求失败: " . $response->statusCode;
}

// GET 请求
Http::get('https://api.example.com/users')
    ->send();

// POST 请求
Http::post('https://api.example.com/users', ['name' => 'John'])
    ->send();

// PUT 请求
Http::put('https://api.example.com/users/123', ['name' => 'Jane'])
    ->send();

// PATCH 请求
Http::patch('https://api.example.com/users/123', ['email' => '[email protected]'])
    ->send();

// DELETE 请求
Http::delete('https://api.example.com/users/123')
    ->send();

// HEAD 请求
Http::head('https://api.example.com/users')
    ->send();

// OPTIONS 请求
Http::options('https://api.example.com/users')
    ->send();

// 设置超时时间
Http::get('https://api.example.com/users')
    ->timeout(10.0) // 10秒超时
    ->send();

// 设置多个头部
Http::get('https://api.example.com/users')
    ->header('Authorization', 'Bearer token')
    ->header('User-Agent', 'MyApp/1.0')
    ->header('Accept', 'application/json')
    ->send();

// 批量设置头部
Http::get('https://api.example.com/users')
    ->headers([
        'Authorization' => 'Bearer token',
        'User-Agent' => 'MyApp/1.0',
        'Accept' => 'application/json'
    ])
    ->send();

// 设置请求体
Http::post('https://api.example.com/users')
    ->body(['name' => 'John', 'email' => '[email protected]'])
    ->send();