OpenSwoole\Coroutine\Http\Client->setMethod(...)

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine\Http\Client->setMethod(string $method): void

Parameters

method

The HTTP method of the request (GET, POST, PUT etc.)

Return

None


Description

Set the HTTP method of the request (GET, POST, PUT etc.).

Important: Only the current request that is going to be made is valid, and the method setting will be cleared immediately after sending the request, you must set the method again for any new requests.

You must comply with the HTTP standard for request method naming, if the given method name is incorrect unexpected errors may take place and your HTTP request may be rejected.

Valid HTTP Method Naming

Most common HTTP methods are:

  • GET
  • HEAD
  • POST
  • PUT
  • DELETE
  • CONNECT
  • OPTIONS
  • PATCH

Sometimes method names are referred to as HTTP Verbs, more information on the Mozilla HTTP Methods guide.


Example

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

co::run(function()
{
    $client = new Client('127.0.0.1', 80);

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

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

    // HTTP Request one, sets the method beforehand
    $client->setMethod('DELETE');
    $status = $client->execute('/remove/post/1');
    echo $status . "\n";
    echo $client->body . "\n";

    // HTTP Request 2, must set the method type again
    $client->setMethod('PUT');
    $client->setData(['coding']);
    $status = $client->execute('/user/new/tag');
    echo $status . "\n";
    echo $client->body . "\n";

    // A GET request ignores setMethod and will always use GET
    $client->get('/index.php');
    echo $client->body;

    $client->close();
});
Last updated on September 1, 2022