Bringing JavaScript’s fetch() to PHP (Sort Of)

If you’ve worked across both frontend and backend stacks, you’ve probably felt the contrast. On the frontend, JavaScript offers an elegant experience. Making an HTTP request is as simple as: const res = await fetch('/api/data'); const data = await res.json(); It’s expressive, minimal, and easy to reason about. But then you switch over to PHP, and the flow changes. You reach for a well-established library like Guzzle or Symfony’s HTTP client, and suddenly things feel a bit heavier. There’s nothing wrong with them — in fact, they’re excellent. But the ergonomics are just... different. That friction sparked a small idea. What if PHP had something that felt like JavaScript’s fetch()? A Familiar Syntax in a Different Language That question led to the creation of Fetch PHP, an HTTP client library designed to bring a more fluent and familiar interface to PHP. It started as a personal experiment. The goal wasn’t to build a better HTTP client than existing ones — just one that felt more intuitive to use if you were already accustomed to JavaScript's conventions. Here’s what that looks like in practice: $response = fetch('https://api.example.com/data')->json(); Or, if you prefer working asynchronously with promises: $result = await( fetch('https://api.example.com/data') ->async() ->withHeader('Authorization', 'Bearer ' . $token) ->withTimeout(5) ->json() ); What It Offers Fetch PHP isn’t a rewrite of how HTTP works in PHP — it’s an abstraction over existing standards and tools, with a focus on developer experience. Some of the features include: Fluent, chainable syntax for building requests Support for synchronous and asynchronous workflows, using ReactPHP-style promises Automatic retries with exponential backoff and error handling PSR-7 and PSR-18 compatibility for interoperability with other libraries PSR-3 logging support, including masking of sensitive data Immutable request handling, reducing unintended side effects The goal is to keep things expressive and minimal, while still offering the power and flexibility needed for real-world use. Lessons from Building It The process of building Fetch PHP was a reminder that language differences don’t have to limit developer experience. PHP may not be inherently asynchronous, but with the right tools, it can be. And even if it’s not JavaScript, it can still feel approachable and expressive. It’s easy to underestimate the impact of ergonomics. Developer comfort matters — and sometimes, reaching for a more intuitive syntax can make a meaningful difference in how you approach and enjoy your work. Give It a Try If you’ve ever found yourself wishing PHP had a more streamlined way to work with HTTP, Fetch PHP might be worth a look. Fetch PHP on GitHub Quickstart Guide and Documentation Feedback and contributions are always welcome.

May 12, 2025 - 11:26
 0
Bringing JavaScript’s fetch() to PHP (Sort Of)

If you’ve worked across both frontend and backend stacks, you’ve probably felt the contrast.

On the frontend, JavaScript offers an elegant experience. Making an HTTP request is as simple as:

const res = await fetch('/api/data');
const data = await res.json();

It’s expressive, minimal, and easy to reason about.

But then you switch over to PHP, and the flow changes. You reach for a well-established library like Guzzle or Symfony’s HTTP client, and suddenly things feel a bit heavier. There’s nothing wrong with them — in fact, they’re excellent. But the ergonomics are just... different.

That friction sparked a small idea.

What if PHP had something that felt like JavaScript’s fetch()?

A Familiar Syntax in a Different Language

That question led to the creation of Fetch PHP, an HTTP client library designed to bring a more fluent and familiar interface to PHP.

It started as a personal experiment. The goal wasn’t to build a better HTTP client than existing ones — just one that felt more intuitive to use if you were already accustomed to JavaScript's conventions.

Here’s what that looks like in practice:

$response = fetch('https://api.example.com/data')->json();

Or, if you prefer working asynchronously with promises:

$result = await(
    fetch('https://api.example.com/data')
        ->async()
        ->withHeader('Authorization', 'Bearer ' . $token)
        ->withTimeout(5)
        ->json()
);

What It Offers

Fetch PHP isn’t a rewrite of how HTTP works in PHP — it’s an abstraction over existing standards and tools, with a focus on developer experience.

Some of the features include:

  • Fluent, chainable syntax for building requests
  • Support for synchronous and asynchronous workflows, using ReactPHP-style promises
  • Automatic retries with exponential backoff and error handling
  • PSR-7 and PSR-18 compatibility for interoperability with other libraries
  • PSR-3 logging support, including masking of sensitive data
  • Immutable request handling, reducing unintended side effects

The goal is to keep things expressive and minimal, while still offering the power and flexibility needed for real-world use.

Lessons from Building It

The process of building Fetch PHP was a reminder that language differences don’t have to limit developer experience. PHP may not be inherently asynchronous, but with the right tools, it can be. And even if it’s not JavaScript, it can still feel approachable and expressive.

It’s easy to underestimate the impact of ergonomics. Developer comfort matters — and sometimes, reaching for a more intuitive syntax can make a meaningful difference in how you approach and enjoy your work.

Give It a Try

If you’ve ever found yourself wishing PHP had a more streamlined way to work with HTTP, Fetch PHP might be worth a look.

Feedback and contributions are always welcome.