PHP code example of osirisgate / http-client

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

    

osirisgate / http-client example snippets


use Osirisgate\Component\HttpClient\Request\Http;

$response = Http::get('https://jsonplaceholder.typicode.com/posts');
$data = $response->toArray();

$response = Http::get(
    'https://jsonplaceholder.typicode.com/comments',
    filters: ['postId' => 1]
);

$comments = $response->toArray();

$response = Http::post(
    'https://jsonplaceholder.typicode.com/posts',
    payload: [
        'title' => 'foo',
        'body' => 'bar',
        'userId' => 1
    ]
);

// PUT (Full update)
$response = Http::put('https://jsonplaceholder.typicode.com/posts/1', payload: [
    'id' => 1,
    'title' => 'new title',
    'body' => 'updated content',
    'userId' => 1
]);

// PATCH (Partial update)
$response = Http::patch('https://jsonplaceholder.typicode.com/posts/1', payload: [
    'title' => 'patched title'
]);

$response = Http::delete('https://jsonplaceholder.typicode.com/posts/1');

$response = Http::get(
    'https://api.example.com/data',
    headers: [
        'Authorization' => 'Bearer TOKEN',
        'X-Custom-Header' => 'value'
    ]
);

$response = Http::get(
    'https://api.example.com/data',
    options: [
        'timeout' => 5,
        'verify_peer' => false
    ]
);

use Osirisgate\Core\Exception\ExceptionInterface;
use Osirisgate\Component\HttpClient\Request\Http;

try {
    $response = Http::get('https://api.example.com/invalid');
} catch (ExceptionInterface $e) {
    echo $e->getMessage(); // Not Found
    echo $e->getCode();    // 404
    print_r($e->format());
}

Get specific field from response
  * ex: $value = $response->get('user.firstname') // return user firstname value or null if not found
  * ex: $value = $response->get('user.account.balance') // return user account balance value or null if not found