PHP code example of mjelamanov / psr18-guzzle

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

    

mjelamanov / psr18-guzzle example snippets




use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Request;
use Mjelamanov\GuzzlePsr18\Client;

$yourOptionalConfig = [];

$guzzleClient = new GuzzleClient($yourOptionalConfig);

$client = new Client($guzzleClient); // create an adapter instance
$request = new Request('GET', 'http://example.com'); // A PSR-7 request instance

$response = $client->sendRequest($request); // Sending request

var_dump($response->getStatusCode(), (string) $response->getBody()); // Prints response

...

use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Client\RequestExceptionInterface;

...

try {
    $response = $client->sendRequest($request);
} catch (NetworkExceptionInterface $e) {
    // Network issues
} catch (RequestExceptionInterface $e) {
    // When request is invalid
} catch (ClientExceptionInterface $e) {
    // In all other cases
}
 http_errors