|
1 | | -Multipurpose PHP Rest Client |
2 | | -============================= |
| 1 | +# PHP REST Client |
3 | 2 |
|
4 | | -A multipurpose PHP rest client for consuming RESTful web services. This package is designed to be very simple and easy to use. |
| 3 | +A flexible and robust PHP REST Client with advanced features for handling API requests. |
5 | 4 |
|
6 | | -### Installation |
| 5 | +## Features |
7 | 6 |
|
8 | | -You can install the package via composer: |
| 7 | +- 🚀 Simple and intuitive API |
| 8 | +- 🔄 Automatic retry mechanism for failed requests |
| 9 | +- 📝 Comprehensive logging system |
| 10 | +- ⚡ Custom exception handling |
| 11 | +- 🔒 Configurable request options |
| 12 | +- 🛠 Extensible architecture |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +Install via Composer: |
9 | 17 |
|
10 | 18 | ```bash |
11 | 19 | composer require ay4t/php-rest-client |
12 | 20 | ``` |
13 | 21 |
|
14 | | -### Description |
15 | | -Example usage of the `Client` class. |
| 22 | +## Basic Usage |
16 | 23 |
|
17 | | -### Example Usage |
| 24 | +### Using Config Object (Recommended) |
18 | 25 |
|
19 | 26 | ```php |
| 27 | +use Ay4t\RestClient\Client; |
| 28 | +use Ay4t\RestClient\Config\Config; |
20 | 29 |
|
| 30 | +// Initialize config |
21 | 31 | $config = new Config(); |
22 | | -$config->setBaseUri('https://api.groq.com/openai/v1') |
23 | | - ->setApiKey('your-api-key-here') |
24 | | - // optional |
25 | | - ->setSecretKey('your-secret-key-here'); |
| 32 | +$config->setBaseUri('https://api.example.com') |
| 33 | + ->setApiKey('your-api-key-here'); |
| 34 | + |
| 35 | +$client = new Client($config); |
| 36 | + |
| 37 | +// Make a GET request |
| 38 | +try { |
| 39 | + $response = $client->cmd('GET', 'users'); |
| 40 | + print_r($response); |
| 41 | +} catch (Ay4t\RestClient\Exceptions\ApiException $e) { |
| 42 | + echo "Error: " . $e->getMessage(); |
| 43 | + echo "HTTP Status: " . $e->getHttpStatusCode(); |
| 44 | + echo "Response Body: " . $e->getResponseBody(); |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### Using Array Configuration (Alternative) |
| 49 | + |
| 50 | +```php |
| 51 | +use Ay4t\RestClient\Client; |
| 52 | + |
| 53 | +// Initialize with array config |
| 54 | +$config = [ |
| 55 | + 'base_uri' => 'https://api.example.com', |
| 56 | + 'headers' => [ |
| 57 | + 'Authorization' => 'Bearer your-api-key-here' |
| 58 | + ] |
| 59 | +]; |
26 | 60 |
|
27 | 61 | $client = new Client($config); |
28 | | -$response = $client->cmd('GET', 'models'); |
| 62 | +``` |
| 63 | + |
| 64 | +## Advanced Features |
| 65 | + |
| 66 | +### Custom Logging |
| 67 | + |
| 68 | +```php |
| 69 | +use Ay4t\RestClient\Logger\DefaultLogger; |
| 70 | +use Ay4t\RestClient\Config\Config; |
| 71 | + |
| 72 | +// Setup configuration |
| 73 | +$config = new Config(); |
| 74 | +$config->setBaseUri('https://api.example.com') |
| 75 | + ->setApiKey('your-api-key-here'); |
| 76 | + |
| 77 | +// Custom log file location |
| 78 | +$logger = new DefaultLogger('/path/to/custom.log'); |
| 79 | +$client = new Client($config, $logger); |
| 80 | + |
| 81 | +// Logs will include: |
| 82 | +// - Request details (method, URL, options) |
| 83 | +// - Response status and body |
| 84 | +// - Any errors that occur |
| 85 | +``` |
29 | 86 |
|
30 | | -echo '<pre>'; |
31 | | -print_r($response); |
32 | | -echo '</pre>'; |
| 87 | +### Retry Mechanism |
| 88 | + |
| 89 | +```php |
| 90 | +// Configure retry settings |
| 91 | +$client->setMaxRetries(5) // Maximum number of retry attempts |
| 92 | + ->setRetryDelay(2000); // Delay between retries in milliseconds |
| 93 | + |
| 94 | +// The client will automatically: |
| 95 | +// - Retry failed requests (except 4xx errors) |
| 96 | +// - Wait between attempts |
| 97 | +// - Throw ApiException after all retries fail |
33 | 98 | ``` |
34 | 99 |
|
35 | | -or |
| 100 | +### Request Options |
36 | 101 |
|
37 | 102 | ```php |
38 | | -$cmd = $client->cmd('POST', 'chat/completions', [ |
39 | | - 'model' => 'llama-3.1-70b-versatile', |
40 | | - 'messages' => [ |
41 | | - [ |
42 | | - 'role' => 'user', |
43 | | - 'content' => 'hi, why is sea water salty?', |
44 | | - ] |
| 103 | +// Set global request options |
| 104 | +$client->setRequestOptions([ |
| 105 | + 'timeout' => 30, |
| 106 | + 'verify' => false, // Disable SSL verification |
| 107 | + 'headers' => [ |
| 108 | + 'User-Agent' => 'My Custom User Agent' |
45 | 109 | ] |
46 | 110 | ]); |
| 111 | +``` |
| 112 | + |
| 113 | +### Error Handling |
| 114 | + |
| 115 | +```php |
| 116 | +use Ay4t\RestClient\Exceptions\ApiException; |
| 117 | + |
| 118 | +try { |
| 119 | + $response = $client->cmd('POST', 'users', [ |
| 120 | + 'name' => 'John Doe', |
| 121 | + 'email' => 'john@example.com' |
| 122 | + ]); |
| 123 | +} catch (ApiException $e) { |
| 124 | + // Get detailed error information |
| 125 | + $statusCode = $e->getHttpStatusCode(); |
| 126 | + $responseBody = $e->getResponseBody(); |
| 127 | + $message = $e->getMessage(); |
| 128 | + |
| 129 | + // Handle different status codes |
| 130 | + switch ($statusCode) { |
| 131 | + case 404: |
| 132 | + echo "Resource not found"; |
| 133 | + break; |
| 134 | + case 401: |
| 135 | + echo "Unauthorized access"; |
| 136 | + break; |
| 137 | + default: |
| 138 | + echo "An error occurred: $message"; |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +## Implementing Custom Logger |
| 144 | + |
| 145 | +You can implement your own logger by implementing the `LoggerInterface`: |
| 146 | + |
| 147 | +```php |
| 148 | +use Ay4t\RestClient\Interfaces\LoggerInterface; |
| 149 | + |
| 150 | +class MyCustomLogger implements LoggerInterface |
| 151 | +{ |
| 152 | + public function logRequest(string $method, string $url, array $options): void |
| 153 | + { |
| 154 | + // Your custom request logging logic |
| 155 | + } |
| 156 | + |
| 157 | + public function logResponse(int $statusCode, string $body): void |
| 158 | + { |
| 159 | + // Your custom response logging logic |
| 160 | + } |
| 161 | + |
| 162 | + public function logError(\Throwable $exception): void |
| 163 | + { |
| 164 | + // Your custom error logging logic |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// Use your custom logger |
| 169 | +$client = new Client($config, new MyCustomLogger()); |
| 170 | +``` |
| 171 | + |
| 172 | +## Contributing |
| 173 | + |
| 174 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 175 | + |
| 176 | +## License |
47 | 177 |
|
48 | | -echo '<pre>'; |
49 | | -print_r($cmd); |
50 | | -echo '</pre>'; |
51 | | -``` |
| 178 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments