PHP code example of nelexa / http-client-simple-cache

1. Go to this page and download the library: Download nelexa/http-client-simple-cache 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/ */

    

nelexa / http-client-simple-cache example snippets




$client = new \Nelexa\HttpClient\HttpClient();



$client = new \Nelexa\HttpClient\HttpClient([
    \Nelexa\HttpClient\Options::HEADERS => [
        'User-Agent' => 'TestHttpClient/1.0',
    ],
]);



$client = new \Nelexa\HttpClient\HttpClient();
$result = $client->get($url, [
    \Nelexa\HttpClient\Options::HANDLER_RESPONSE => $callable
]);

function(\Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response){
    return ...;
}



$client = new \Nelexa\HttpClient\HttpClient();

// use \Closure handler

$base64Contents = $client->get($url, [
    \Nelexa\HttpClient\Options::HANDLER_RESPONSE => static function (Psr\Http\Message\RequestInterface $request, Psr\Http\Message\ResponseInterface $response) {
        return base64_encode($response->getBody()->getContents());
    },
]);

// or use class handler

$base64Contents = $client->get($url, [
    \Nelexa\HttpClient\Options::HANDLER_RESPONSE => new class() implements \Nelexa\HttpClient\ResponseHandlerInterface {
    
        public function __invoke(Psr\Http\Message\RequestInterface $request, Psr\Http\Message\ResponseInterface $response)
        {
            return base64_encode($response->getBody()->getContents());
        }
    },
]);



class Api
{
    /** @var \Nelexa\HttpClient\HttpClient */
    private $httpClient;

    public function __construct(Psr\SimpleCache\CacheInterface $cache)
    {
        $this->httpClient = new \Nelexa\HttpClient\HttpClient([], $cache);
    }

    /**
     * Fetch uuid.
     *
     * @return string
     */
    public function fetchUUID(): string
    {
        return $this->httpClient->request('GET', 'https://httpbin.org/uuid', [
            
            \Nelexa\HttpClient\Options::CACHE_TTL => \DateInterval::createFromDateString('1 min'), // eConnection('redis://localhost')
    )
);

$api = new Api($cache);
$UUID = $api->fetchUUID();

\PHPUnit\Framework\Assert::assertSame($api->fetchUUID(), $UUID);

var_dump($UUID); // string(36) "a72b27c2-7e69-4bc8-8d5b-ccb0e496a7bf"



$urls = [
    'jpeg' => 'https://httpbin.org/image/jpeg',
    'png' => 'https://httpbin.org/image/png',
    'webp' => 'https://httpbin.org/image/webp',
];

$client = new \Nelexa\HttpClient\HttpClient();
$result = $client->requestAsyncPool('GET', $urls, [
    \Nelexa\HttpClient\Options::HANDLER_RESPONSE => static function (Psr\Http\Message\RequestInterface $request, Psr\Http\Message\ResponseInterface $response) {
        return getimagesizefromstring($response->getBody()->getContents());
    },
], $concurrency = 2);

print_r($result);

// Output:
//
//Array
//(
//    [png] => Array
//    (
//            [0] => 100
//            [1] => 100
//            [2] => 3
//            [3] => width="100" height="100"
//            [bits] => 8
//            [mime] => image/png
//    )
//
//    [webp] => Array
//    (
//            [0] => 274
//            [1] => 367
//            [2] => 18
//            [3] => width="274" height="367"
//            [bits] => 8
//            [mime] => image/webp
//    )
//
//    [jpeg] => Array
//    (
//            [0] => 239
//            [1] => 178
//            [2] => 2
//            [3] => width="239" height="178"
//            [bits] => 8
//            [channels] => 3
//            [mime] => image/jpeg
//    )
//)