- Lightweight: Less than 500 lines of code
- Zero Dependencies: No external dependencies required
- PHP 7.1+ Compatible: Works with modern PHP versions
- Multiple HTTP Methods: Supports GET, POST, PUT, PATCH, DELETE
- State Management: Preserves and restores PHP superglobals
- Error Handling: Comprehensive error handling and validation
- Stream Simulation: Mock php://input stream for request body handling
- PHP 7.1 or higher
- Composer (recommended) or manual installation
Install by running:
composer require lfvcodes/microbridge-phpIf you're not using Composer, you can manually integrate MicroBridge into your project by following these steps:
Download or clone the repository and copy the src/ folder into your project:
your-project/
├── src/
│ ├── MicroBridge.php
│ ├── RequestContext.php
│ └── MockPhpStream.php
└── your-code.php
In your main file (index.php or similar), include the class file and use the namespace:
<?php
require_once 'src/MicroBridge.php';
require_once 'src/RequestContext.php';
require_once 'src/MockPhpStream.php';
use MicroBridge\MicroBridge;
// Your code here...
?><?php
require 'vendor/autoload.php';
use MicroBridge\MicroBridge;
try {
// Create client with POST method
$client = new MicroBridge('POST');
// Make request with data payload
$response = $client->request('./api/users.php', [
'name' => 'John Doe',
'email' => 'john@example.com'
]);
print_r($response);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?><?php
require 'vendor/autoload.php';
use MicroBridge\MicroBridge;
try {
$client = new MicroBridge('GET');
// Method 1: URL with query string
$response = $client->request('./api/users.php?id=4');
// Method 2: Separate parameters
$response = $client->request('./api/users.php', ['id' => 4]);
print_r($response);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?><?php
require 'vendor/autoload.php';
use MicroBridge\MicroBridge;
try {
$client = new MicroBridge('PUT');
$response = $client->request('./api/users.php',
['id' => 1, 'name' => 'Updated Name'],
['Authorization' => 'Bearer token123']
);
print_r($response);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?>The examples/ directory contains working demonstrations of MicroBridge functionality. These examples are fully runnable and show real-world usage patterns.
basic-usage.php- Demonstrates fundamental GET, POST, PUT requestschained-requests.php- Shows how to chain multiple API calls togethererror-handling.php- Illustrates comprehensive error handling scenarios
Navigate to the project root directory and run any example:
# Run basic usage examples
php examples/basic-usage.php
# Run chained requests demonstration
php examples/chained-requests.php
# Run error handling examples
php examples/error-handling.phpIf you have a local web server (Apache, Nginx, or PHP's built-in server) configured to serve PHP files:
# Start PHP's built-in development server
php -S localhost:8000
# Then visit in your browser:
# http://localhost:8000/examples/basic-usage.php
# http://localhost:8000/examples/chained-requests.php
# http://localhost:8000/examples/error-handling.phpWhen running basic-usage.php, you should see clean HTML output demonstrating:
- Successful GET requests with chained API calls
- POST request handling with JSON payloads
- PUT requests with custom headers
- Proper error handling for invalid methods
All examples include the working API endpoints (api/api1.php and api/api2.php) that demonstrate real internal API communication without network overhead.
| Method | Description | Use Case |
|---|---|---|
GET |
Retrieve data | Fetching resources |
POST |
Create new resource | Creating new records |
PUT |
Update entire resource | Full resource updates |
PATCH |
Partial update | Updating specific fields |
DELETE |
Remove resource | Deleting records |
composer test# Check code style
composer cs-check
# Fix code style issues
composer cs-fix- 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
This project is licensed under the MIT License - see the LICENSE file for details.
- Built for modern PHP microservice architectures
- Inspired by the need for lightweight internal API communication
- Designed with simplicity and performance in mind