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.
composer require jerome/fetch-php
Read the quick start guide to begin working with Fetch PHP.