Swoole\Coroutine\Http\Client->post(...)

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Coroutine\Http\Client->post(string $path, mixed $data): void

Parameters

path

The URL path to send a POST request to. For example /user/account or /index.php. Only set the path here not the protocol or domain name like http://domain.

data

The POST data to be sent with the HTTP request. This can be text or binary data, so you can pass arrays or strings, which will get put inside the body of the HTTP request.

Return

None


Description

Send a HTTP POST request to the remote server. Initiates a POST request which contains data within the HTTP body. When using arrays, the request and data will automatically be formatted using x-www-form-urlencoded and its Content-Type set to the same.

Using this method will ignore anything set using setMethod() as only POST will be used


Example

<?php
use Swoole\Coroutine\HTTP\Client;

Co\run(function()
{
    $client = new Client('127.0.0.1', 80);

    $client->setHeaders([
        'Host' => "localhost",
        "User-Agent" => 'Chrome/49.0.2587.3',
        'Accept' => 'text/html,application/xhtml+xml,application/xml',
        'Accept-Encoding' => 'gzip',
    ]);

    $client->set(['timeout' => 1]);

    // Will automatically use and set x-www-form-urlencoded
    $client->post('/index.php', ['a'=> 123,'b'=>"hey"]);

    echo $client->body;

    $client->close();
});
Last updated on August 31, 2022