amirxd / php-ultra-request
A powerful HTTP client library for PHP
dev-main
2026-07-08 14:12 UTC
Requires
- php: >=8.0
- ext-curl: *
- ext-json: *
This package is auto-updated.
Last update: 2026-07-08 14:18:07 UTC
README
A powerful, feature-rich HTTP client library for PHP with full cURL support, cookie management, proxy handling, authentication, and advanced file downloading capabilities.
โจ Features
- ๐ Full cURL Integration - Leverages PHP's cURL extension for maximum performance
- ๐ช Cookie Management - Full cookie jar with persistence, parsing, and domain/path matching
- ๐ Proxy Support - HTTP, HTTPS, SOCKS4, SOCKS5 proxies with authentication
- ๐ Authentication - Basic, Bearer Token, and API Key authentication methods
- ๐ฅ Advanced Downloader - Resume support, progress tracking, console progress bar, retry mechanism
- ๐ Manual Redirect Handling - RFC 7231 compliant, preserves method semantics (301/302/307/308)
- ๐งน Clean Architecture - PSR-4 compliant, fully object-oriented with interfaces
- โก Fluent Interface - Method chaining for elegant API usage
- ๐งช JSON Support - Automatic JSON encoding/decoding with type safety
- ๐ SSL Verification - Optional SSL verification for development/secure environments
๐ Requirements
- PHP 8.0 or higher
- cURL extension (
ext-curl) - JSON extension (
ext-json)
๐ Installation
Via Composer
composer require amirxd/php-ultra-request
Manual Installation
git clone https://github.com/amirxd/php-ultra-request.git
cd php-ultra-request
composer install
๐ Quick Start
Basic GET Request
<?php require_once 'vendor/autoload.php'; use Amirxd\UltraRequest\UltraRequest; $http = new UltraRequest(); $response = $http->get('https://api.github.com/users/amirxd'); if ($response->isSuccessful()) { $data = $response->getJson(); echo "User: " . ($data['name'] ?? 'N/A') . "\n"; echo "Bio: " . ($data['bio'] ?? 'N/A') . "\n"; }
POST Request with JSON
<?php use Amirxd\UltraRequest\UltraRequest; $http = new UltraRequest(); $data = [ 'title' => 'New Post', 'body' => 'Lorem ipsum dolor sit amet', 'userId' => 1 ]; $response = $http->post( 'https://jsonplaceholder.typicode.com/posts', json_encode($data), ['Content-Type' => 'application/json'] ); if ($response->isSuccessful()) { $result = $response->getJson(); echo "Created post ID: " . $result['id'] . "\n"; }
Using the Fluent Request Builder
<?php use Amirxd\UltraRequest\Request\Request; use Amirxd\UltraRequest\Client\Client; $client = new Client(); $request = Request::post('https://api.example.com/users') ->withHeader('X-API-Key', 'your-api-key-here') ->withJson(['name' => 'John Doe', 'email' => 'john@example.com']) ->withQueryParams(['source' => 'web']); $response = $client->send($request);
๐ Authentication
Basic Authentication
<?php use Amirxd\UltraRequest\UltraRequest; $http = new UltraRequest(); $client = $http->getClient()->withBasicAuth('username', 'password'); $response = $client->send( Request::get('https://api.example.com/protected') );
Bearer Token (OAuth2)
<?php use Amirxd\UltraRequest\UltraRequest; $http = new UltraRequest(); $client = $http->getClient()->withBearerAuth('your-token-here'); $response = $client->send( Request::get('https://api.example.com/user') );
API Key Authentication
<?php use Amirxd\UltraRequest\Auth\AuthManager; use Amirxd\UltraRequest\Client\Client; $auth = new AuthManager(); $auth->apiKey('X-API-Key', 'your-api-key', 'header'); $client = (new Client())->withAuth(function($manager) { $manager->apiKey('X-API-Key', 'your-api-key', 'header'); }); $response = $client->send( Request::get('https://api.example.com/protected') );
Using AuthManager with Fluent API
<?php use Amirxd\UltraRequest\Client\Client; $client = new Client(); $client = $client->withAuth(function($auth) { $auth->basic('username', 'password'); // OR $auth->bearer('your-token-here'); // OR $auth->apiKey('X-API-Key', 'your-key', 'header'); });
๐ช Cookie Management
Basic Cookie Usage
<?php use Amirxd\UltraRequest\Cookie\CookieJar; use Amirxd\UltraRequest\Client\Client; use Amirxd\UltraRequest\Request\Request; $cookieJar = new CookieJar('cookies.json', true); // Auto-load from file $client = (new Client())->withCookieJar($cookieJar); // Cookies will be automatically saved and loaded $response = $client->send(Request::get('https://example.com')); // Manually set a cookie $cookieJar->setCookie('session_id', 'abc123', [ 'domain' => '.example.com', 'path' => '/', 'expires' => time() + 3600, 'secure' => true, 'httpOnly' => true ]); // Get all cookies as header string $cookieHeader = $cookieJar->toHeader();
Cookie Persistence
<?php // Save cookies to file $cookieJar = new CookieJar('cookies.json'); $cookieJar->setCookie('user', 'john_doe'); // Load existing cookies $cookieJar = new CookieJar('cookies.json', true); echo $cookieJar->getCookie('user')->getValue(); // john_doe
๐ Proxy Support
HTTP/HTTPS Proxy
<?php use Amirxd\UltraRequest\Proxy\Proxy; use Amirxd\UltraRequest\Client\Client; $proxy = new Proxy('http://proxy.example.com:8080'); // With authentication $proxy = (new Proxy('http://proxy.example.com:8080')) ->withAuth('username', 'password'); $client = (new Client())->setProxy($proxy);
SOCKS Proxy
<?php use Amirxd\UltraRequest\Proxy\Proxy; // SOCKS5 proxy $proxy = new Proxy('socks5://socks.example.com:1080'); // SOCKS4 proxy $proxy = new Proxy('socks4://socks.example.com:1080'); $client = (new Client())->setProxy($proxy);
Proxy String Formats
// All these formats are supported: $proxy = new Proxy('http://proxy.example.com:8080'); $proxy = new Proxy('proxy.example.com:8080'); // Defaults to HTTP $proxy = new Proxy('https://proxy.example.com:443'); $proxy = new Proxy('socks5://user:pass@socks.example.com:1080');
๐ฅ Advanced File Downloader
Basic Download
<?php use Amirxd\UltraRequest\File\Downloader; use Amirxd\UltraRequest\Client\Client; $downloader = new Downloader( 'https://example.com/file.zip', '/path/to/save/file.zip' ); $client = new Client(); $client->download($downloader);
Download with Progress Bar (Console)
<?php use Amirxd\UltraRequest\File\Downloader; use Amirxd\UltraRequest\Client\Client; $downloader = new Downloader( 'https://example.com/large-file.zip', './downloads/file.zip' ); // Enable console progress bar $downloader = $downloader->withConsoleProgress(50); $client = new Client(); $client->download($downloader); // Output: [โโโโโโโโโโโโโโโโโโโโโโโโโโโ] 75.0% | 15.3 MB / 20.4 MB | 2.1 MB/s | ETA: 00:24
Download with Custom Progress Callback
<?php use Amirxd\UltraRequest\File\Downloader; use Amirxd\UltraRequest\Client\Client; $downloader = new Downloader( 'https://example.com/file.zip', './downloads/file.zip' ); $downloader = $downloader->withProgress(function($progress) { echo sprintf( "Downloaded: %s / %s (%.1f%%) | Speed: %s | ETA: %s\n", $progress['downloaded_formatted'], $progress['total_formatted'], $progress['percent'], $progress['speed_formatted'], $progress['eta_formatted'] ); }); $client = new Client(); $client->download($downloader);
Resume Download
<?php use Amirxd\UltraRequest\File\Downloader; use Amirxd\UltraRequest\Client\Client; $downloader = new Downloader( 'https://example.com/large-file.zip', './downloads/file.zip' ); // Enable resume support $downloader = $downloader->withResume(true); $client = new Client(); $client->download($downloader); // Will resume if file exists
Download with Retry Mechanism
<?php use Amirxd\UltraRequest\File\Downloader; use Amirxd\UltraRequest\Client\Client; $downloader = new Downloader( 'https://example.com/file.zip', './downloads/file.zip' ); // Retry up to 3 times with 2-second delay between attempts $downloader = $downloader->withRetries(3, 2); $client = new Client(); $client->download($downloader);
Complete Download Example
<?php use Amirxd\UltraRequest\File\Downloader; use Amirxd\UltraRequest\Client\Client; try { $downloader = new Downloader( 'https://example.com/large-file.zip', './downloads/large-file.zip' ); $downloader = $downloader ->withResume(true) ->withConsoleProgress(50) ->withRetries(3, 2) ->withOverwrite(false) // Don't overwrite if exists (unless resuming) ->withHeader('User-Agent', 'PHP Ultra Request') ->withChunkSize(1024 * 1024 * 2); // 2MB chunks $client = new Client(); $success = $client->download($downloader); if ($success) { echo "\nโ Download completed!\n"; } } catch (\Exception $e) { echo "\nโ Download failed: " . $e->getMessage() . "\n"; }
โ๏ธ Advanced Client Configuration
Custom Headers
<?php use Amirxd\UltraRequest\Client\Client; use Amirxd\UltraRequest\Request\Request; $client = new Client(); $client = $client->withAuth(function($auth) { $auth->bearer('your-token'); }); $request = Request::get('https://api.example.com/users') ->withHeader('X-Custom-Header', 'custom-value') ->withHeader('Accept', 'application/json'); $response = $client->send($request);
Timeout Configuration
<?php use Amirxd\UltraRequest\Client\Client; use Amirxd\UltraRequest\Request\Request; $client = (new Client()) ->withTimeout(60) // Total timeout: 60 seconds ->withConnectTimeout(10); // Connection timeout: 10 seconds $response = $client->send(Request::get('https://slow-api.example.com'));
SSL Verification Control
<?php use Amirxd\UltraRequest\Client\Client; // Enable SSL verification (default, recommended for production) $client = (new Client())->withSSL(true); // Disable SSL verification (useful for development) $client = (new Client())->withSSL(false);
Custom cURL Options
<?php use Amirxd\UltraRequest\Client\Client; use Amirxd\UltraRequest\Request\Request; $client = (new Client()) ->withCurlOption(CURLOPT_USERAGENT, 'MyCustomBot/1.0') ->withCurlOption(CURLOPT_MAXREDIRS, 5) ->withCurlOption(CURLOPT_REFERER, 'https://google.com'); $response = $client->send(Request::get('https://example.com'));
๐ Response Handling
Response Methods
<?php use Amirxd\UltraRequest\UltraRequest; $http = new UltraRequest(); $response = $http->get('https://api.example.com/data'); // Get status code $statusCode = $response->getStatusCode(); // 200 // Get response body $body = $response->getBody(); // Raw string // Get JSON decoded data $data = $response->getJson(true); // Associative array $data = $response->getJson(false); // Object // Check response status if ($response->isSuccessful()) { // 2xx responses } elseif ($response->isRedirect()) { // 3xx responses } elseif ($response->isClientError()) { // 4xx responses } elseif ($response->isServerError()) { // 5xx responses } // Get specific header $contentType = $response->getHeader('Content-Type'); $allHeaders = $response->getHeaders(); // Get cURL info $info = $response->getInfo(); // URL, total_time, etc.
๐งช Testing
Run Tests
# Download a test file php tests/DownloadTest.php # Run the basic test script php tests/UltraRequestTest.php
Example Test Script
<?php // tests/UltraRequestTest.php require_once 'vendor/autoload.php'; use Amirxd\UltraRequest\UltraRequest; echo "๐งช Testing UltraRequest...\n\n"; try { $http = new UltraRequest(); // Test GET echo "๐ก Testing GET request...\n"; $response = $http->get('https://httpbin.org/get'); echo "โ Status: " . $response->getStatusCode() . "\n"; // Test POST echo "\n๐ก Testing POST request...\n"; $response = $http->post('https://httpbin.org/post', json_encode(['test' => 'success']), ['Content-Type' => 'application/json'] ); echo "โ Status: " . $response->getStatusCode() . "\n"; echo "\nโจ All tests passed!\n"; } catch (\Exception $e) { echo "โ Error: " . $e->getMessage() . "\n"; exit(1); }
๐ Directory Structure
php-ultra-request/
โโโ src/
โ โโโ Auth/
โ โ โโโ ApiKeyAuth.php
โ โ โโโ AuthInterface.php
โ โ โโโ AuthManager.php
โ โ โโโ BasicAuth.php
โ โ โโโ BearerAuth.php
โ โโโ Client/
โ โ โโโ Client.php
โ โโโ Cookie/
โ โ โโโ Cookie.php
โ โ โโโ CookieInterface.php
โ โ โโโ CookieJar.php
โ โ โโโ CookieJarInterface.php
โ โโโ File/
โ โ โโโ Downloader.php
โ โโโ Proxy/
โ โ โโโ Proxy.php
โ โ โโโ ProxyInterface.php
โ โโโ Request/
โ โ โโโ Request.php
โ โ โโโ RequestInterface.php
โ โโโ Response/
โ โ โโโ HttpResponse.php
โ โ โโโ ResponseInterface.php
โ โโโ UltraRequest.php
โโโ tests/
โ โโโ UltraRequestTest.php
โโโ examples/
โ โโโ basic-usage.php
โ โโโ download-example.php
โ โโโ cookie-example.php
โโโ composer.json
โโโ LICENSE
โโโ README.md
โโโ .gitignore
๐ค Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Coding Standards
- Follow PSR-12 coding standards
- Add tests for new features
- Update documentation accordingly
- Ensure PHP 8.0+ compatibility
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Built with PHP's powerful cURL extension
- Inspired by Guzzle HTTP Client
- RFC 7231 compliant for HTTP/1.1
๐ซ Contact
- Author: Amirxd
- Email: amirxd4488.pm@example.com
- GitHub: github.com/amirxd
๐ Star the Project
If you find this library useful, please consider giving it a โญ on GitHub!
Made with โค๏ธ by Amirxd