PHP code example of imahmood / laravel-http-client

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

    

imahmood / laravel-http-client example snippets


$body = [
    'username' => '__USERNAME__',
    'password' => '__PASSWORD__',
];

$request = new Request(HttpMethod::POST, 'https://example.com/api/auth/login', $body);
$response = ClientFactory::create()->send($request);

$request = new Request(HttpMethod::POST, 'https://example.com/api/users/1/avatar', $body);
$request->addFile('avatar', '/home/user/avatar.png');

$response = ClientFactory::create()->send($request);

$accessToken = '__TOKEN__';

$request = new Request(HttpMethod::GET, 'https://example.com/api/users', $body);
$response = ClientFactory::createWithBearerToken($accessToken)->send($request);

$response = ClientFactory::create()->send($request);

echo $response->duration();

$response = ClientFactory::create()->send($request);

try {
    $response->validate();
} catch (\Imahmood\HttpClient\Exceptions\ServerException $e) {
    // Handle the exception here
} catch (\Imahmood\HttpClient\Exceptions\ClientException $e) {
    // Handle the exception here
} catch (\Illuminate\Validation\ValidationException $e) {
    // Handle the exception here
}