This content originally appeared on DEV Community and was authored by Jerome Thayananthajothy
Hey PHP developers! đ
Today, Iâm excited to introduce FetchPHP, a lightweight HTTP library for PHP that takes direct inspiration from JavaScriptâs fetch
API. If youâve ever found yourself making HTTP requests in PHP and thinking "I wish this was as simple as JavaScriptâs fetch," then this package is for you!
đĄ What is FetchPHP?
FetchPHP is a new, modern HTTP library built on top of Guzzle, designed to make working with HTTP requests in PHP both simple and powerful. Just like its JavaScript inspiration, FetchPHP allows you to send asynchronous or synchronous requests, easily handle JSON, multipart, or form data, and comes with built-in status helpers to make handling responses a breeze.
⨠Key Features
Hereâs a quick overview of the features FetchPHP brings to the table:
-
Synchronous and Asynchronous Support: You can choose between
fetch
(synchronous) andfetchAsync
(asynchronous), giving you flexibility based on your needs. -
Familiar API: If youâre used to JavaScriptâs
fetch
API, youâll feel right at home. FetchPHPâs API mirrors that simplicity and ease of use. - Support for JSON, Multipart, and Form Data: Easily handle different types of requests and responses, whether youâre sending JSON or uploading files.
-
Handy Status Helpers: FetchPHP includes built-in helpers like
ok()
,isClientError()
,isServerError()
, and more to simplify handling response statuses. - Easy Authentication and Proxies: Need to authenticate or route through a proxy? FetchPHP has you covered.
đ Getting Started
FetchPHP is available via Composer, so adding it to your project is super simple. Hereâs how to get started:
- Install FetchPHP:
composer require yourusername/fetchphp
- Make a Request:
<?php
require 'vendor/autoload.php';
use function FetchPHP\fetch;
$response = fetch('https://jsonplaceholder.typicode.com/todos/1');
// Get the JSON response
$data = $response->json();
print_r($data);
In the example above, weâre making a simple GET request and parsing the JSON response. Youâll notice that FetchPHP makes working with HTTP requests in PHP as clean as working with the fetch
API in JavaScript.
đ ď¸ Example Use Cases
Making a POST Request with JSON Data
Need to send data to an API? Hereâs how easy it is to use FetchPHP for a POST
request:
$response = fetch('https://jsonplaceholder.typicode.com/posts', [
'method' => 'POST',
'json' => [
'title' => 'New Post',
'body' => 'This is the content of the new post.',
'userId' => 1,
],
]);
echo $response->statusText(); // Should print "Created"
print_r($response->json());
Handling Asynchronous Requests
FetchPHP also supports asynchronous requests using fetchAsync
. This is especially useful for performance-critical tasks where you donât want to block execution:
$promise = fetchAsync('https://jsonplaceholder.typicode.com/todos/1');
$promise->then(function ($response) {
print_r($response->json());
});
$promise->wait(); // Wait for the request to finish
Multipart Form Data for File Uploads
Uploading files is made easy with FetchPHPâs support for multipart form data:
$response = fetch('https://example.com/upload', [
'method' => 'POST',
'multipart' => [
[
'name' => 'file',
'contents' => fopen('/path/to/file.jpg', 'r'),
'filename' => 'file.jpg'
]
]
]);
echo $response->statusText(); // Outputs the HTTP status text
đ§ Under the Hood
FetchPHP leverages Guzzle under the hood, one of the most powerful and flexible HTTP clients for PHP. This means you get all the benefits of Guzzle (performance, reliability, rich feature set) with a simpler and more intuitive API inspired by JavaScriptâs fetch
.
âď¸ Support the Project
Iâm really excited to share this project with the PHP community, and Iâd love your support! Head over to GitHub, give the project a âď¸, and share your feedback. Whether itâs bug reports, feature requests, or pull requests, all contributions are welcome! đ
đ˘ Conclusion
FetchPHP aims to make HTTP requests in PHP as easy and familiar as JavaScriptâs fetch
. Itâs simple to use, yet powerful enough for any PHP application. I hope it saves you time and effort in your projects. Thanks for checking it out!
đ Useful Links
- GitHub: GitHub Repository
- Packagist: FetchPHP on Packagist
Thanks for reading, and happy coding! đ
This content originally appeared on DEV Community and was authored by Jerome Thayananthajothy
Jerome Thayananthajothy | Sciencx (2024-09-13T20:42:27+00:00) đ Introducing FetchPHP: A Simple, Powerful HTTP Library for PHP, Inspired by JavaScriptâs fetch API. Retrieved from https://www.scien.cx/2024/09/13/%f0%9f%9a%80-introducing-fetchphp-a-simple-powerful-http-library-for-php-inspired-by-javascripts-fetch-api/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.