Swoole\Coroutine\Http\Client->getBody()

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->getBody(): string|bool

Parameters

None

Return

A string which contains the HTTP body response from a request or false if something went wrong.


Description

Get the HTTP body content returned from the remote server from a request.


Example

Using a body method

<?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]);

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

    echo $client->getBody();

    $client->close();
});

Using the class body property

When performing a request like get() the body is automatically stored in the body class property, this allows us to access the contents directly.This property type is a string.

<?php

use Swoole\Coroutine\Http\Client;

Co\run(function()
{
    $client = new Client('https://swoole.co.uk', 443);

    $client->get('/get');

    // The body is automatically updated, we can access it directly
    echo $client->body;

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