PHP code example of riftfox / php-http-client

1. Go to this page and download the library: Download riftfox/php-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/ */

    

riftfox / php-http-client example snippets


namespace App\Http;

use Riftfox\HttpClient\ResultInterface;
use Riftfox\HttpClient\ResultFactoryInterface;
use Psr\Http\Message\ResponseInterface;

class UserResult implements ResultInterface
{
    public array $data;

    public function __construct(array $data)
    {
        $this->data = $data;
    }
}

class UserResultFactory implements ResultFactoryInterface
{
    public function create(ResponseInterface $response): ResultInterface
    {
        $json = (string) $response->getBody();
        $data = json_decode($json, true);
        
        return new UserResult($data ?? []);
    }
}

use Riftfox\HttpClient\HttpClient;
use GuzzleHttp\Client as GuzzleClient; // Example PSR-18 Client
use GuzzleHttp\Psr7\Request;

// 1. Initialize the PSR-18 client and your wrapper
$psrClient = new GuzzleClient();
$client = new HttpClient($psrClient);

// 2. Prepare the request and the factory
$request = new Request('GET', 'https://api.example.com/users/1');
$factory = new UserResultFactory();

// 3. Send request and get the transformed result directly
/** @var UserResult $result */
$result = $client->send($request, $factory);

print_r($result->data);