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

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

Declaration

<?php OpenSwoole\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 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->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 OpenSwoole\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 September 1, 2022