Fetch PHP is a Lightweight HTTP Library Inspired by JavaScript's fetch()

Fetch PHP is a lightweight HTTP library inspired by JavaScript's fetch, bringing simplicity and flexibility to PHP HTTP requests. It uses the Guzzle client behind the scenes, offering synchronous and asynchronous requests with an easy-to-use API I know that Guzzle is king, and I will use Laravel's HTTP client on most projects. However, the Fetch PHP package is just downright fun when you want a no-frills fetch() function: $response = fetch('https://jsonplaceholder.typicode.com/todos/1'); // Get the JSON response $data = $response->json(assoc: true); print_r($data); /* [ "userId" => 1, "id" => 1, "title" => "delectus aut autem", "completed" => false } */ // Get the status text (e.g., "OK") echo $response->statusText(); Available Response Methods json(bool $assoc = true): Decodes the response body as JSON. If $assoc is true, it returns an associative array. If false, it returns an object. text(): Returns the res

Fetch PHP is a Lightweight HTTP Library Inspired by JavaScript's fetch()


Click the link to join the registration on WhatsApp: https://chat.whatsapp.com/KThkuZlWaTcCu3iJHGBmLM


Click the link to join the registration on WhatsApp: https://chat.whatsapp.com/KThkuZlWaTcCu3iJHGBmLM


Click the link to join the registration on WhatsApp: https://chat.whatsapp.com/KThkuZlWaTcCu3iJHGBmLM

Fetch PHP is a Lightweight HTTP Library Inspired by JavaScript's fetch()

Fetch PHP is a lightweight HTTP library inspired by JavaScript's fetch, bringing simplicity and flexibility to PHP HTTP requests. It uses the Guzzle client behind the scenes, offering synchronous and asynchronous requests with an easy-to-use API

I know that Guzzle is king, and I will use Laravel's HTTP client on most projects. However, the Fetch PHP package is just downright fun when you want a no-frills fetch() function:

$response = fetch('https://jsonplaceholder.typicode.com/todos/1');

// Get the JSON response
$data = $response->json(assoc: true);
print_r($data);
/*
[
  "userId" => 1,
  "id" => 1,
  "title" => "delectus aut autem",
  "completed" => false
}
*/

// Get the status text (e.g., "OK")
echo $response->statusText();

Available Response Methods

  • json(bool $assoc = true): Decodes the response body as JSON. If $assoc is true, it returns an associative array. If false, it returns an object.
  • text(): Returns the response body as plain text.
  • blob(): Returns the response body as a PHP stream resource (like a "blob").
  • arrayBuffer(): Returns the response body as a binary string.
  • statusText(): Returns the HTTP status text (e.g., "OK" for 200).
  • ok(): Returns true if the status code is between 200-299.
  • isInformational(), isRedirection(), isClientError(), isServerError(): Helpers to check status ranges.

The asynchronous requests use the fetchAsync() function to can be used as follows:

//
// Asyc requests
//
$promise = fetchAsync('https://jsonplaceholder.typicode.com/todos/1');

$promise->then(function ($response) {
    $data = $response->json();
    print_r($data);
});

// Wait for the promise to resolve
$promise->wait();

//
// Error handling
//
$promise = fetchAsync('https://nonexistent-url.com');

$promise->then(function ($response) {
    // handle success
}, function ($exception) {
    // handle failure
    echo "Request failed: " . $exception->getMessage();
});

You can also pass Guzzle options to the fetch() and fetchAsync() functions for any advanced Guzzle features you'll need. You can learn more about this package, get full installation instructions, and view the source code on GitHub.


The post Fetch PHP is a Lightweight HTTP Library Inspired by JavaScript's fetch() appeared first on Laravel News.

Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow

Keep advertising to get more people

You could reach thousands of more people for every ₦1,000 you spend. https://doacweb.com/advertising

Adverts on doacWeb can be informative, educative or persuasive in nature.

doacWeb Ads is always directed at a broad audience (reaching thousands of people day by day), not few individuals — it deliver your advert to the target audience at the same time. Putting your offer in front of the right people — who have the money and interest in what you sell.

doacWeb Advertising gives you advantage as adverts passes through https://doacweb.com to the internet, reaching millions of people over the internet, and to grow your audience.

Grow your business, Be known, Boost your visibility, Drive engagements, Get new customers and Increase sales.

doacWeb acts as a global advertising media, to let people — individuals — and businesses, to promote and reach more interested people.

Get Started.

WhatsApp: 09031633831

Email: info@doacweb.com
Keep advertising to get more people

You could reach thousands of more people for every ₦1,000 you spend. https://doacweb.com/advertising

Adverts on doacWeb can be informative, educative or persuasive in nature.

doacWeb Ads is always directed at a broad audience (reaching thousands of people day by day), not few individuals — it deliver your advert to the target audience at the same time. Putting your offer in front of the right people — who have the money and interest in what you sell.

doacWeb Advertising gives you advantage as adverts passes through https://doacweb.com to the internet, reaching millions of people over the internet, and to grow your audience.

Grow your business, Be known, Boost your visibility, Drive engagements, Get new customers and Increase sales.

doacWeb acts as a global advertising media, to let people — individuals — and businesses, to promote and reach more interested people.

Get Started.

WhatsApp: 09031633831

Email: info@doacweb.com
Keep advertising to get more people

You could reach thousands of more people for every ₦1,000 you spend. https://doacweb.com/advertising

Adverts on doacWeb can be informative, educative or persuasive in nature.

doacWeb Ads is always directed at a broad audience (reaching thousands of people day by day), not few individuals — it deliver your advert to the target audience at the same time. Putting your offer in front of the right people — who have the money and interest in what you sell.

doacWeb Advertising gives you advantage as adverts passes through https://doacweb.com to the internet, reaching millions of people over the internet, and to grow your audience.

Grow your business, Be known, Boost your visibility, Drive engagements, Get new customers and Increase sales.

doacWeb acts as a global advertising media, to let people — individuals — and businesses, to promote and reach more interested people.

Get Started.

WhatsApp: 09031633831

Email: info@doacweb.com