gnu-social/docs/developer/src/httpclient.md
2021-09-14 13:13:29 +01:00

1.3 KiB

HTTP Client

It is sometimes necessary to perform HTTP requests. We simply have a static wrapper around Symfony HTTP.

You can do App\Core\HTTPClient::{METHOD}(string: $url, array: $options): ResponseInterface. The $options are elaborated in Symfony Doc.

Please note that the HTTP Client is lazy, which makes it very important to bear in mind Network Errors,

An example where this behaviour has to be considered:

if (Common::isValidHttpUrl($url)) {
    $head = HTTPClient::head($url);
    // This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec)
    try {
        $headers = $head->getHeaders();
    } catch (ClientException $e) {
        throw new InvalidArgumentException(previous: $e);
    }
    $url      = $head->getInfo('url'); // The last effective url (after getHeaders, so it follows redirects)
    $url_hash = hash(self::URLHASH_ALGO, $url);
} else {
    throw new InvalidArgumentException();
}

What you can take from Responses is specified here.