Familiar API
If you know JavaScript's fetch() API, you'll feel right at home with Fetch PHP's intuitive interface.
The JavaScript fetch API for PHP
Modern, simple HTTP client with a familiar API
// Quick API requests with fetch()
$response = fetch('https://api.example.com/users');
$users = $response->json();
// Or use HTTP method helpers
$user = post('https://api.example.com/users', [
'name' => 'John Doe',
'email' => 'john@example.com'
])->json();
// Bearer token auth
$response = fetch('https://api.example.com/me', [
'token' => 'your-oauth-token'
]);
// Basic auth
$response = fetch('https://api.example.com/private', [
'auth' => ['username', 'password']
]);
// Create promises for parallel requests
$usersPromise = async(function() {
return fetch('https://api.example.com/users');
});
$postsPromise = async(function() {
return fetch('https://api.example.com/posts');
});
// Wait for all to complete
all(['users' => $usersPromise, 'posts' => $postsPromise])
->then(function ($results) {
// Process results from both requests
$users = $results['users']->json();
$posts = $results['posts']->json();
});
await(async(function() {
// Process multiple requests in parallel
$results = await(all([
'users' => async(fn() => fetch('https://api.example.com/users')),
'posts' => async(fn() => fetch('https://api.example.com/posts'))
]));
// Work with results as if they were synchronous
foreach ($results['users']->json() as $user) {
echo $user['name'] . "\n";
}
}));
Fetch PHP brings the simplicity of JavaScript's fetch API to PHP, while adding powerful features like retry handling, promise-based asynchronous requests, and fluent interface for request building. It's designed to be both simple for beginners and powerful for advanced users.
Key Benefits:
composer require jerome/fetch-php
Read the quick start guide to begin working with Fetch PHP.
While Guzzle is a powerful HTTP client, Fetch PHP enhances the experience by providing a JavaScript-like API, global client management, simplified requests, enhanced error handling, and modern PHP 8.1+ enums.
Yes! Fetch PHP works seamlessly with all PHP frameworks including Laravel, Symfony, CodeIgniter, and others. It requires PHP 8.1 or higher.
Absolutely. Fetch PHP provides an elegant API for file uploads, supporting both single and multiple file uploads with progress tracking.
Yes. Fetch PHP is built on top of Guzzle, one of the most battle-tested HTTP clients in the PHP ecosystem, while providing a more modern developer experience.
Having trouble? Open an issue on our GitHub repository.