PHP code example of codecoz / laravel-rest-client

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

    

codecoz / laravel-rest-client example snippets



return [
    'base_url' => env('REST_CLIENT_BASE_URL', 'https://api.example.com'),

    'default_auth' => [
        'type' => env('REST_CLIENT_AUTH_TYPE', 'bearer'),
        'header' => env('REST_CLIENT_AUTH_HEADER', 'Authorization'),
        'token' => env('REST_CLIENT_AUTH_TOKEN'),
        'param_key' => env('REST_CLIENT_API_KEY_PARAM', 'api_key'),
        'provider' => null  // \App\Services\MyTokenProvider::class
    ],

    'logging' => [
        'enabled' => env('REST_CLIENT_LOGGING', true),
        'sanitize'=> env('REST_CLIENT_LOG_SANITIZE', true),
        'payload' => env('REST_CLIENT_LOG_PAYLOAD', true),
        'channel' => env('REST_CLIENT_LOG_CHANNEL', 'stack'),
        'level'   => env('REST_CLIENT_LOG_LEVEL', 'debug'),
    ],

    'caching' => [
        'enabled' => env('REST_CLIENT_CACHING', false),
        'store' =>   env('REST_CLIENT_CACHE_STORE', 'file'),
        'ttl' =>     env('REST_CLIENT_CACHE_LIFETIME', 3600), // 1 hour default
    ],
];


use CodeCoz\Laravel\RestClient\Facades\RestClient;

// GET request
$response = RestClient::get('/posts');

// GET with query parameters
RestClient::get('/posts', ['page' => 2]);

// POST request
RestClient::post('/posts', ['title' => 'New Post', 'body' => '...']);

// Form data
RestClient::asForm()->post('/login', [
    'email' => '[email protected]',
    'password' => 'secret'
]);

// Chain options
RestClient::cache(600)
          ->withoutLogging()
          ->get('/expensive-endpoint');
 

// config/rest-client.php
'auth' => [
    'type' => 'bearer',
    'token' => 'your-global-token-here',
]

RestClient::withToken('temporary-token')->get('/protected-resource');

RestClient::withoutLogging()->post('/webhook', $payload);



// Cache response for 10 minutes
RestClient::cache(600)->get('/slow-endpoint');

// Disable caching for a specific call
RestClient::withoutCache()->get('/real-time-data');



use CodeCoz\Laravel\RestClient\Facades\RestClient;
use CodeCoz\Laravel\RestClient\Exceptions\{
    ClientErrorException,
    ServerErrorException,
    ConnectionException,
    RestClientException
};

try {
    $data = RestClient::get('/users/999')->json();
} catch (ClientErrorException $e) {
    // 4xx errors (401, 403, 404, 422, etc.)
    // Access API error details: $e->getResponseData()
    return response()->json(['error' => $e->getMessage()], $e->getCode());

} catch (ServerErrorException $e) {
    // 5xx errors
    \Log::error('External API error', ['exception' => $e]);

} catch (ConnectionException $e) {
    // Network / timeout / DNS issues
    return response()->json(['error' => 'Service unavailable'], 502);

} catch (RestClientException $e) {
    // Any other API-related error
}


// app/Services/GitHubApiClient.php
namespace App\Services;

use CodeCoz\Laravel\RestClient\Facades\RestClient;

class GitHubApiClient
{
    public function __construct()
    {
        RestClient::withToken(config('services.github.token'))
                  ->baseUrl('https://api.github.com');
    }

    public function getUser(string $username)
    {
        return RestClient::cache(300)
                         ->get("/users/{$username}")
                         ->json();
    }

    public function getRepos(string $username)
    {
        return RestClient::get("/users/{$username}/repos")->json();
    }

    public function createIssue(string $owner, string $repo, array $data)
    {
        return RestClient::post("/repos/{$owner}/{$repo}/issues", $data)->json();
    }
}


$github = new App\Services\GitHubApiClient();
$user = $github->getUser('torvalds');