PHP code example of liborm85 / loggable-http-client

1. Go to this page and download the library: Download liborm85/loggable-http-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

liborm85 / loggable-http-client example snippets




$httpClient = \Symfony\Component\HttpClient\HttpClient::create(); // optional
$loggableHttpClient = new \Liborm85\LoggableHttpClient\LoggableHttpClient($httpClient);
$loggableHttpClient->setLogger(new \MyLogger());

$response = $loggableHttpClient->request('GET', 'https://example.com');


class MyLogger extends \Psr\Log\AbstractLogger
{

    public function log($level, $message, array $context = []): void
    {
        if (isset($context['request']) && ($context['request'] instanceof \Liborm85\LoggableHttpClient\Context\RequestContextInterface)) {
            $context['request']->getContent(); // request content body as string
            (string)$context['request']; // is Stringable, request content body as string
            $context['request']->toStream(); // request content body as PHP stream
            $context['request']->getHeadersAsString(); // request headers as string
            $context['request']->getHeaders(); // request headers as array (string[][])
            $context['request']->getRequestTime(); // request time as DateTimeInterface
            $context['request']->getRequestMethod(); // request HTTP method
            $context['request']->getUrl(); // full request URL
        }

        if (isset($context['response']) && ($context['response'] instanceof \Liborm85\LoggableHttpClient\Context\ResponseContextInterface)) {
            $context['response']->getContent(); // response content body as string
            (string)$context['response']; // is Stringable, response content body as string
            $context['response']->toStream(); // response content body as PHP stream
            $context['response']->getHeadersAsString(); // response headers as string
            $context['response']->getHeaders(); // response headers as array (string[][])
            $context['response']->getResponseTime(); // response time as DateTimeInterface
        }

        if (isset($context['info']) && ($context['info'] instanceof \Liborm85\LoggableHttpClient\Context\InfoContextInterface)) {
            $context['info']->getInfo(); // return all available information
            $context['info']->getInfo('url'); // return one information for provided type
        }
    }

}