Swoole HTTP Client Helper Functions

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

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);
    });
});
Last updated on August 31, 2022