Skip to content

Fetch PHP

The JavaScript fetch API for PHP

Modern, simple HTTP client with a familiar API

Fetch PHP

The Modern HTTP Client for PHP ​

php
// 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();

Flexible Authentication ​

php
// 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']
]);

Powerful Async Support ​

php
// 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();
    });

Modern Await-Style Syntax ​

php
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";
    }
}));

Why Fetch PHP? ​

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:

  • JavaScript-like syntax that's familiar to full-stack developers
  • Promise-based API with .then(), .catch(), and .finally() methods
  • Built on Guzzle for rock-solid performance with an elegant API
  • Type-safe enums for HTTP methods, content types, and status codes
  • Automatic retry mechanics with exponential backoff

Getting Started ​

bash
composer require jerome/fetch-php

Read the quick start guide to begin working with Fetch PHP.

Frequently Asked Questions ​

How does Fetch PHP compare to Guzzle? ​

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.

Can I use Fetch PHP with Laravel or Symfony? ​

Yes! Fetch PHP works seamlessly with all PHP frameworks including Laravel, Symfony, CodeIgniter, and others. It requires PHP 8.1 or higher.

Does Fetch PHP support file uploads? ​

Absolutely. Fetch PHP provides an elegant API for file uploads, supporting both single and multiple file uploads with progress tracking.

Is Fetch PHP suitable for production use? ​

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.

Released under the MIT License. A modern HTTP client for PHP developers.