Skip to content

Latest commit

History

History
46 lines (35 loc) 路 978 Bytes

http-request.md

File metadata and controls

46 lines (35 loc) 路 978 Bytes

HTTP requests

The request() function

The request() function allows to make HTTP requests easily. It performs HTTP request and returns an instance of Symfony\Contracts\HttpClient\ResponseInterface:

use Castor\Attribute\AsTask;

use function Castor\io;
use function Castor\request;

#[AsTask()]
function foo()
{
    io()->writeln(request('GET', 'https://example.org')->getContent());
}

The http_client() function

If you need to have a full control on the HTTP client, you can access the HttpClientInterface directly with the http_client() function:

use Castor\Attribute\AsTask;

use function Castor\http_client;

#[AsTask()]
function foo()
{
    $client = http_client()
        ->withOptions([
            'verify_peer' => false,
            'timeout' => 10,
        ])
    ;
}

You can check the Symfony documentation for more information about this component and how to use it.