Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A lightweight, flexible PHP REST client for consuming RESTful web services. Designed for simplicity and ease of use, this package offers a clean interface for making API requests with customizable configurations.

Notifications You must be signed in to change notification settings

ay4t/php-rest-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

8 Commits

Repository files navigation

PHP REST Client

A flexible and robust PHP REST Client with advanced features for handling API requests.

Features

  • πŸš€ Simple and intuitive API
  • πŸ”„ Automatic retry mechanism for failed requests
  • πŸ“ Comprehensive logging system
  • ⚑ Custom exception handling
  • πŸ”’ Configurable request options
  • πŸ›  Extensible architecture

Installation

Install via Composer:

composer require ay4t/php-rest-client

Basic Usage

Using Config Object (Recommended)

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();
}

Using Array Configuration (Alternative)

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);

Advanced Features

Custom Logging

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 include:
// - Request details (method, URL, options)
// - Response status and body
// - Any errors that occur

Retry Mechanism

// 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

Request Options

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

Error Handling

use Ay4t\RestClient\Exceptions\ApiException;
try {
 $response = $client->cmd('POST', 'users', [
 'name' => 'John Doe',
 'email' => 'john@example.com'
 ]);
} 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";
 }
}

Implementing Custom Logger

You can implement your own logger by implementing the LoggerInterface:

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());

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A lightweight, flexible PHP REST client for consuming RESTful web services. Designed for simplicity and ease of use, this package offers a clean interface for making API requests with customizable configurations.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /