OpenSwoole\Coroutine\Http\Client->getStatusCode()

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

Declaration

<?php OpenSwoole\Coroutine\Http\Client->getStatusCode(): int|bool

Parameters

None

Return

An integer of the HTTP status code or false if something went wrong.


Description

Get the HTTP status code returned from the remote server. If the status code is negative it means there is a problem with the connection.

Negative Status Code Errors

  • -1: OpenSwoole\Constant::HTTP_CLIENT_ESTATUS_CONNECT_FAILED

    • The connection timed out, the server does not monitor the port or the network and then the connection is lost, you can read $client->errCode to get the specific network error code.
  • -2: OpenSwoole\Constant::HTTP_CLIENT_ESTATUS_REQUEST_TIMEOUT

    • The request timed out and the server did not return a response within the specified timeout time.
  • -3: OpenSwoole\Constant::HTTP_CLIENT_ESTATUS_SERVER_RESET

    • After the client request is sent, the server forcibly cuts off the connection.
  • -4: OpenSwoole\Constant::HTTP_CLIENT_ESTATUS_SEND_FAILED

    • The client failed to send data to the remote host. Check $client->errCode for specific error code and message.

Example

<?php
use OpenSwoole\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]);

    $client->setDefer(true);

    $client->get('/index.php');

    var_dump($client->getStatusCode());

    $client->close();
});

You can also access the HTTP client status code by using the class property directly like so:

Swoole\Coroutine\Http\Client->statusCode: int

echo $client->statusCode;
Last updated on September 20, 2022