Introducing Fetch PHP 3.0: JavaScript-like HTTP Requests for Modern PHP Applications

We're excited to announce the release of Fetch PHP 3.0, a major update to our HTTP client library that brings the intuitive experience of JavaScript's fetch() API to PHP developers. This release represents a significant advancement in how PHP applications can handle HTTP requests, with powerful new features for both synchronous and asynchronous operations. The Vision Behind Fetch PHP When we first created Fetch PHP, our goal was simple: to bring the elegance and simplicity of JavaScript's fetch API to the PHP ecosystem. Frontend developers have long enjoyed the intuitive nature of fetch() for making HTTP requests, and we wanted backend developers to have the same experience. With version 3.0, we've taken this vision even further by enhancing the library with more powerful features while maintaining the clean, intuitive API that made it popular. What's New in Version 3.0 True Asynchronous Support Fetch PHP 3.0 introduces full asynchronous request capabilities powered by our custom Matrix async engine. This allows PHP developers to write non-blocking code with JavaScript-like syntax: // Provided by Matrix PHP (https://github.com/Thavarshan/matrix) use function async; use function await; // Promise-based approach async(fn () => fetch('https://api.example.com/users')) ->then(fn ($response) => $response->json()) ->catch(fn ($error) => handleError($error)); // Or using async/await pattern $response = await(async(fn () => fetch('https://api.example.com/users'))); $users = $response->json(); This async capability is game-changing for PHP applications that need to perform multiple concurrent operations, such as fetching data from several APIs or services simultaneously. Enhanced Fluent API The fluent API has been significantly improved in version 3.0, offering more flexibility and cleaner syntax: $response = fetch() ->baseUri('https://api.example.com') ->withHeaders(['Accept' => 'application/json']) ->withToken('your-access-token') ->withJson(['name' => 'John Doe', 'email' => 'john@example.com']) ->post('/users'); This chainable approach makes building complex requests intuitive and readable, reducing the cognitive load when working with HTTP requests. Advanced Concurrency Controls Version 3.0 introduces robust tools for managing concurrent requests, inspired by JavaScript's Promise API: use function async; use function await; use function all; // Run 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')), 'comments' => async(fn () => fetch('https://api.example.com/comments')) ])); // Access results $users = $results['users']->json(); $posts = $results['posts']->json(); This pattern is ideal for dashboard applications, aggregation services, or any scenario where you need to collect data from multiple sources efficiently. Intelligent Retry Mechanism Fetch PHP 3.0 includes a sophisticated retry system with exponential backoff: $response = fetch() ->retry(3, 100) // 3 retries with 100ms initial delay ->get('https://api.example.com/unstable-endpoint'); The library automatically retries failed requests with increasing delays between attempts, making your applications more resilient against transient network issues or service outages. Comprehensive Response Object The Response class has been enhanced with more methods for working with different content types: $response = fetch('https://api.example.com/users/1'); // JSON handling $user = $response->json(); // Status checking if ($response->isOk()) { // Success } elseif ($response->isNotFound()) { // Handle 404 } // Header access $contentType = $response->header('Content-Type'); The response object now implements ArrayAccess, allowing you to use array syntax to access JSON response data directly: $userId = $response['id']; $userName = $response['name']; Performance Improvements Performance has been a key focus for version 3.0. We've optimized the core request handling to reduce overhead, resulting in: Faster response times for synchronous requests Lower memory usage during concurrent operations Better handling of large response bodies More efficient connection pooling These improvements make Fetch PHP 3.0 suitable for high-performance applications that need to process many requests quickly and efficiently. Upgrading from Previous Versions For users of Fetch PHP 2.x, upgrading to version 3.0 is straightforward in most cases. We've maintained backwards compatibility with the core API while adding new features. A few breaking changes were necessary to support the new async capabilities, but our migration guide provides detailed instructions for a smooth transition. Getting Started Ready to try Fetch PHP 3.0? Ins

May 4, 2025 - 12:10
 0
Introducing Fetch PHP 3.0: JavaScript-like HTTP Requests for Modern PHP Applications

We're excited to announce the release of Fetch PHP 3.0, a major update to our HTTP client library that brings the intuitive experience of JavaScript's fetch() API to PHP developers. This release represents a significant advancement in how PHP applications can handle HTTP requests, with powerful new features for both synchronous and asynchronous operations.

The Vision Behind Fetch PHP

When we first created Fetch PHP, our goal was simple: to bring the elegance and simplicity of JavaScript's fetch API to the PHP ecosystem. Frontend developers have long enjoyed the intuitive nature of fetch() for making HTTP requests, and we wanted backend developers to have the same experience.

With version 3.0, we've taken this vision even further by enhancing the library with more powerful features while maintaining the clean, intuitive API that made it popular.

What's New in Version 3.0

True Asynchronous Support

Fetch PHP 3.0 introduces full asynchronous request capabilities powered by our custom Matrix async engine. This allows PHP developers to write non-blocking code with JavaScript-like syntax:

// Provided by Matrix PHP (https://github.com/Thavarshan/matrix)
use function async;
use function await;

// Promise-based approach
async(fn () => fetch('https://api.example.com/users'))
    ->then(fn ($response) => $response->json())
    ->catch(fn ($error) => handleError($error));

// Or using async/await pattern
$response = await(async(fn () => fetch('https://api.example.com/users')));
$users = $response->json();

This async capability is game-changing for PHP applications that need to perform multiple concurrent operations, such as fetching data from several APIs or services simultaneously.

Enhanced Fluent API

The fluent API has been significantly improved in version 3.0, offering more flexibility and cleaner syntax:

$response = fetch()
    ->baseUri('https://api.example.com')
    ->withHeaders(['Accept' => 'application/json'])
    ->withToken('your-access-token')
    ->withJson(['name' => 'John Doe', 'email' => 'john@example.com'])
    ->post('/users');

This chainable approach makes building complex requests intuitive and readable, reducing the cognitive load when working with HTTP requests.

Advanced Concurrency Controls

Version 3.0 introduces robust tools for managing concurrent requests, inspired by JavaScript's Promise API:

use function async;
use function await;
use function all;

// Run 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')),
    'comments' => async(fn () => fetch('https://api.example.com/comments'))
]));

// Access results
$users = $results['users']->json();
$posts = $results['posts']->json();

This pattern is ideal for dashboard applications, aggregation services, or any scenario where you need to collect data from multiple sources efficiently.

Intelligent Retry Mechanism

Fetch PHP 3.0 includes a sophisticated retry system with exponential backoff:

$response = fetch()
    ->retry(3, 100)  // 3 retries with 100ms initial delay
    ->get('https://api.example.com/unstable-endpoint');

The library automatically retries failed requests with increasing delays between attempts, making your applications more resilient against transient network issues or service outages.

Comprehensive Response Object

The Response class has been enhanced with more methods for working with different content types:

$response = fetch('https://api.example.com/users/1');

// JSON handling
$user = $response->json();

// Status checking
if ($response->isOk()) {
    // Success
} elseif ($response->isNotFound()) {
    // Handle 404
}

// Header access
$contentType = $response->header('Content-Type');

The response object now implements ArrayAccess, allowing you to use array syntax to access JSON response data directly:

$userId = $response['id'];
$userName = $response['name'];

Performance Improvements

Performance has been a key focus for version 3.0. We've optimized the core request handling to reduce overhead, resulting in:

  • Faster response times for synchronous requests
  • Lower memory usage during concurrent operations
  • Better handling of large response bodies
  • More efficient connection pooling

These improvements make Fetch PHP 3.0 suitable for high-performance applications that need to process many requests quickly and efficiently.

Upgrading from Previous Versions

For users of Fetch PHP 2.x, upgrading to version 3.0 is straightforward in most cases. We've maintained backwards compatibility with the core API while adding new features. A few breaking changes were necessary to support the new async capabilities, but our migration guide provides detailed instructions for a smooth transition.

Getting Started

Ready to try Fetch PHP 3.0? Installation is simple with Composer:

composer require jerome/fetch-php

Our comprehensive documentation includes a quick start guide, detailed API reference, and numerous examples to help you get up and running quickly.

The Future of Fetch PHP

With version 3.0, Fetch PHP has matured into a robust, feature-rich HTTP client that bridges the gap between frontend and backend development patterns. We're committed to continuing this evolution with future enhancements focused on:

  • Expanded middleware support
  • Additional content type handlers
  • More advanced caching mechanisms
  • Enhanced debugging tools

Conclusion

Fetch PHP 3.0 represents a significant step forward in how PHP developers can interact with HTTP resources. By combining the familiar, intuitive syntax of JavaScript's fetch API with the power and flexibility of PHP, we've created a library that makes complex HTTP operations simpler and more maintainable.

Whether you're building APIs, integrating with third-party services, or developing microservices, Fetch PHP 3.0 provides the tools you need to handle HTTP requests elegantly and efficiently.

We're excited to see what you build with it!

Fetch PHP is an open-source project. We welcome contributions, feedback, and feature requests on our GitHub repository.