PHP code example of ay4t / php-rest-client

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

    

ay4t / php-rest-client example snippets


use Ay4t\RestClient\Client;
use Ay4t\RestClient\Config\Config;

// Initialize config
$config = new Config();
$config->setBaseUri('https://api.example.com')
       ->setApiKey('your-api-key-here');

$client = new Client($config);

// Make a GET request
try {
    $response = $client->cmd('GET', 'users');
    print_r($response);
} catch (Ay4t\RestClient\Exceptions\ApiException $e) {
    echo "Error: " . $e->getMessage();
    echo "HTTP Status: " . $e->getHttpStatusCode();
    echo "Response Body: " . $e->getResponseBody();
}

use Ay4t\RestClient\Client;

// Initialize with array config
$config = [
    'base_uri' => 'https://api.example.com',
    'headers' => [
        'Authorization' => 'Bearer your-api-key-here'
    ]
];

$client = new Client($config);

use Ay4t\RestClient\Logger\DefaultLogger;
use Ay4t\RestClient\Config\Config;

// Setup configuration
$config = new Config();
$config->setBaseUri('https://api.example.com')
       ->setApiKey('your-api-key-here');

// Custom log file location
$logger = new DefaultLogger('/path/to/custom.log');
$client = new Client($config, $logger);

// Logs will 

// Configure retry settings
$client->setMaxRetries(5)      // Maximum number of retry attempts
       ->setRetryDelay(2000);  // Delay between retries in milliseconds

// The client will automatically:
// - Retry failed requests (except 4xx errors)
// - Wait between attempts
// - Throw ApiException after all retries fail

// Set global request options
$client->setRequestOptions([
    'timeout' => 30,
    'verify' => false,  // Disable SSL verification
    'headers' => [
        'User-Agent' => 'My Custom User Agent'
    ]
]);

use Ay4t\RestClient\Exceptions\ApiException;

try {
    $response = $client->cmd('POST', 'users', [
        'name' => 'John Doe',
        'email' => '[email protected]'
    ]);
} catch (ApiException $e) {
    // Get detailed error information
    $statusCode = $e->getHttpStatusCode();
    $responseBody = $e->getResponseBody();
    $message = $e->getMessage();
    
    // Handle different status codes
    switch ($statusCode) {
        case 404:
            echo "Resource not found";
            break;
        case 401:
            echo "Unauthorized access";
            break;
        default:
            echo "An error occurred: $message";
    }
}

use Ay4t\RestClient\Interfaces\LoggerInterface;

class MyCustomLogger implements LoggerInterface
{
    public function logRequest(string $method, string $url, array $options): void
    {
        // Your custom request logging logic
    }

    public function logResponse(int $statusCode, string $body): void
    {
        // Your custom response logging logic
    }

    public function logError(\Throwable $exception): void
    {
        // Your custom error logging logic
    }
}

// Use your custom logger
$client = new Client($config, new MyCustomLogger());