PHP code example of setono / http-client-bundle

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

    

setono / http-client-bundle example snippets



// config/bundles.php

return [
    // ...

    Setono\HttpClientBundle\SetonoHttpClientBundle::class => ['all' => true],

    // ...
];


use Setono\HttpClientBundle\HttpClient\RequestAwareHttpClient;
use Setono\HttpClientBundle\HttpClient\RequestAwareHttpClientInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class YourService
{
    private RequestAwareHttpClientInterface $httpClient;
    
    public function __construct(HttpClientInterface $httpClient)
    {
        $this->httpClient = new RequestAwareHttpClient($httpClient);
    }
    
    public function doSomething(): void
    {
        $response = $this->httpClient->request('POST', 'https://httpbin.org/post', [
            'json' => ['name' => 'John Doe']
        ]);

        $request = $this->httpClient->getRequestFromResponse($response);

        echo $request->asString();
        
        // Outputs:
        // POST https://httpbin.org/post {"name":"John Doe"}
    }
}