Swoole HTTP Client Helper Functions

Notice: ext-swoole is supported until v4.7.1, use ext-openswoole >= v4.7.1. Latest version: pecl install openswoole-4.9.1

The helper request functions allow you to make one off HTTP requests quickly and easily, they are useful for when you don't need the full power of the HTTP client.

There are 3 main functions and they are request(), post() and get().


Take a look at the full example below:

<?php

use function Swoole\Coroutine\go;
use function Swoole\Coroutine\Http\get;
use function Swoole\Coroutine\Http\post;
use function Swoole\Coroutine\Http\request;

// Main coroutine context
Co\run(function()
{
    // Coroutine 1
    go(function()
    {
        $data = get('http://httpbin.org/get?hello=world');
        $body = json_decode($data->getBody());
        assert($body->headers->Host === 'httpbin.org');
        assert($body->args->hello === 'world');
    });

    // Coroutine 2
    go(function()
    {
        $random_data = base64_encode(random_bytes(128));
        $data = post('http://httpbin.org/post?hello=world', ['random_data' => $random_data]);
        $body = json_decode($data->getBody());
        assert($body->headers->Host === 'httpbin.org');
        assert($body->args->hello === 'world');
        assert($body->form->random_data === $random_data);
    });
});