friendship ended with social-app. php is my new best friend
1---
2layout: home
3
4# SEO-optimized title and description
5title: Fetch PHP - Modern JavaScript-like HTTP Client for PHP
6description: A modern HTTP client library that brings JavaScript's fetch API experience to PHP with async/await patterns, promise-based API, and powerful retry mechanics.
7
8# SEO-optimized meta tags (these extend what's in config.js)
9head:
10 - - meta
11 - name: keywords
12 content: fetch php, javascript fetch in php, async await php, http client php, promise based http, php http, guzzle alternative
13 - - meta
14 - property: og:title
15 content: Fetch PHP - JavaScript's fetch API for PHP
16 - - meta
17 - property: og:description
18 content: Write HTTP requests just like you would in JavaScript with PHP's Fetch API. Modern, simple HTTP client with a familiar API.
19 - - link
20 - rel: canonical
21 href: https://fetch-php.thavarshan.com/
22
23# Your existing hero section (unchanged)
24hero:
25 name: Fetch PHP
26 text: The JavaScript fetch API for PHP
27 tagline: Modern, simple HTTP client with a familiar API
28 image:
29 src: /logo.png
30 alt: Fetch PHP
31 actions:
32 - theme: brand
33 text: Get Started
34 link: /guide/quickstart
35 - theme: alt
36 text: View on GitHub
37 link: https://github.com/Thavarshan/fetch-php
38
39# Your existing features section (unchanged)
40features:
41 - title: Familiar API
42 details: If you know JavaScript's fetch() API, you'll feel right at home with Fetch PHP's intuitive interface.
43 icon: 🚀
44 - title: Promise-Based Async
45 details: Support for async/await-style programming with promises for concurrent HTTP requests.
46 icon: ⚡
47 - title: Fluent Interface
48 details: Chain methods together for clean, expressive code that's easy to read and maintain.
49 icon: 🔗
50 - title: Helper Functions
51 details: Simple global helpers like get(), post(), and fetch() for quick and easy HTTP requests.
52 icon: 🧰
53 - title: PSR Compatible
54 details: Implements PSR-7 (HTTP Messages), PSR-18 (HTTP Client), and PSR-3 (Logging) standards.
55 icon: 🔄
56 - title: Powerful Responses
57 details: Rich Response objects with methods for JSON parsing, XML handling, and more.
58 icon: 📦
59---
60
61<!-- The rest of your existing content starts here, properly formatted with Markdown -->
62
63## The Modern HTTP Client for PHP
64
65```php
66// Quick API requests with fetch()
67$response = fetch('https://api.example.com/users');
68$users = $response->json();
69
70// Or use HTTP method helpers
71$user = post('https://api.example.com/users', [
72 'name' => 'John Doe',
73 'email' => 'john@example.com'
74])->json();
75```
76
77### Flexible Authentication
78
79```php
80// Bearer token auth
81$response = fetch('https://api.example.com/me', [
82 'token' => 'your-oauth-token'
83]);
84
85// Basic auth
86$response = fetch('https://api.example.com/private', [
87 'auth' => ['username', 'password']
88]);
89```
90
91### Powerful Async Support
92
93```php
94// Create promises for parallel requests
95$usersPromise = async(function() {
96 return fetch('https://api.example.com/users');
97});
98
99$postsPromise = async(function() {
100 return fetch('https://api.example.com/posts');
101});
102
103// Wait for all to complete
104all(['users' => $usersPromise, 'posts' => $postsPromise])
105 ->then(function ($results) {
106 // Process results from both requests
107 $users = $results['users']->json();
108 $posts = $results['posts']->json();
109 });
110```
111
112### Modern Await-Style Syntax
113
114```php
115await(async(function() {
116 // Process multiple requests in parallel
117 $results = await(all([
118 'users' => async(fn() => fetch('https://api.example.com/users')),
119 'posts' => async(fn() => fetch('https://api.example.com/posts'))
120 ]));
121
122 // Work with results as if they were synchronous
123 foreach ($results['users']->json() as $user) {
124 echo $user['name'] . "\n";
125 }
126}));
127```
128
129## Why Fetch PHP?
130
131Fetch 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.
132
133<div class="custom-block tip">
134 <p><strong>Key Benefits:</strong></p>
135 <ul>
136 <li>JavaScript-like syntax that's familiar to full-stack developers</li>
137 <li>Promise-based API with .then(), .catch(), and .finally() methods</li>
138 <li>Built on Guzzle for rock-solid performance with an elegant API</li>
139 <li>Type-safe enums for HTTP methods, content types, and status codes</li>
140 <li>Automatic retry mechanics with exponential backoff</li>
141 </ul>
142</div>
143
144## Getting Started
145
146```bash
147composer require jerome/fetch-php
148```
149
150Read the [quick start guide](/guide/quickstart) to begin working with Fetch PHP.
151
152<!-- Add FAQ section for long-tail SEO keywords -->
153## Frequently Asked Questions
154
155### How does Fetch PHP compare to Guzzle?
156
157While 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.
158
159### Can I use Fetch PHP with Laravel or Symfony?
160
161Yes! Fetch PHP works seamlessly with all PHP frameworks including Laravel, Symfony, CodeIgniter, and others. It requires PHP 8.1 or higher.
162
163### Does Fetch PHP support file uploads?
164
165Absolutely. Fetch PHP provides an elegant API for file uploads, supporting both single and multiple file uploads with progress tracking.
166
167### Is Fetch PHP suitable for production use?
168
169Yes. 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.
170
171<div class="custom-block warning">
172 <p>Having trouble? <a href="https://github.com/Thavarshan/fetch-php/issues">Open an issue</a> on our GitHub repository.</p>
173</div>