friendship ended with social-app. php is my new best friend
1--- 2title: HTTP Method Helpers API Reference 3description: API reference for the HTTP method helpers in the Fetch HTTP client package 4--- 5 6# HTTP Method Helpers 7 8The Fetch package provides a set of convenient global helper functions for making HTTP requests with different HTTP methods. These helper functions make your code more readable and expressive by providing simplified shortcuts for common operations. 9 10## Function Signatures 11 12### `get()` 13 14Perform a GET request. 15 16```php 17/** 18 * @param string $url URL to fetch 19 * @param array<string, mixed>|null $query Query parameters 20 * @param array<string, mixed>|null $options Additional request options 21 * @return ResponseInterface The response 22 * 23 * @throws ClientExceptionInterface If a client exception occurs 24 */ 25function get(string $url, ?array $query = null, ?array $options = []): ResponseInterface 26``` 27 28### `post()` 29 30Perform a POST request. 31 32```php 33/** 34 * @param string $url URL to fetch 35 * @param mixed $data Request body or JSON data 36 * @param array<string, mixed>|null $options Additional request options 37 * @return ResponseInterface The response 38 * 39 * @throws ClientExceptionInterface If a client exception occurs 40 */ 41function post(string $url, mixed $data = null, ?array $options = []): ResponseInterface 42``` 43 44### `put()` 45 46Perform a PUT request. 47 48```php 49/** 50 * @param string $url URL to fetch 51 * @param mixed $data Request body or JSON data 52 * @param array<string, mixed>|null $options Additional request options 53 * @return ResponseInterface The response 54 * 55 * @throws ClientExceptionInterface If a client exception occurs 56 */ 57function put(string $url, mixed $data = null, ?array $options = []): ResponseInterface 58``` 59 60### `patch()` 61 62Perform a PATCH request. 63 64```php 65/** 66 * @param string $url URL to fetch 67 * @param mixed $data Request body or JSON data 68 * @param array<string, mixed>|null $options Additional request options 69 * @return ResponseInterface The response 70 * 71 * @throws ClientExceptionInterface If a client exception occurs 72 */ 73function patch(string $url, mixed $data = null, ?array $options = []): ResponseInterface 74``` 75 76### `delete()` 77 78Perform a DELETE request. 79 80```php 81/** 82 * @param string $url URL to fetch 83 * @param mixed $data Request body or JSON data 84 * @param array<string, mixed>|null $options Additional request options 85 * @return ResponseInterface The response 86 * 87 * @throws ClientExceptionInterface If a client exception occurs 88 */ 89function delete(string $url, mixed $data = null, ?array $options = []): ResponseInterface 90``` 91 92## Examples 93 94### GET Request 95 96```php 97// Simple GET request 98$response = get('https://api.example.com/users'); 99 100// GET request with query parameters 101$response = get('https://api.example.com/users', [ 102 'page' => 1, 103 'limit' => 10, 104 'sort' => 'name' 105]); 106 107// GET request with additional options 108$response = get('https://api.example.com/users', ['page' => 1], [ 109 'headers' => [ 110 'X-API-Key' => 'your-api-key' 111 ], 112 'timeout' => 5 113]); 114 115// Process the response 116$users = $response->json(); 117foreach ($users as $user) { 118 echo $user['name'] . "\n"; 119} 120``` 121 122### POST Request 123 124```php 125// POST request with JSON data 126$response = post('https://api.example.com/users', [ 127 'name' => 'John Doe', 128 'email' => 'john@example.com' 129]); 130 131// POST request with a string body 132$response = post('https://api.example.com/raw', 'Raw request body'); 133 134// POST request with additional options 135$response = post('https://api.example.com/users', 136 ['name' => 'John Doe'], 137 [ 138 'headers' => [ 139 'X-Custom-Header' => 'value' 140 ], 141 'timeout' => 10 142 ] 143); 144 145// Check if the request was successful 146if ($response->isSuccess()) { 147 $user = $response->json(); 148 echo "Created user with ID: " . $user['id']; 149} 150``` 151 152### PUT Request 153 154```php 155// PUT request to update a resource 156$response = put('https://api.example.com/users/1', [ 157 'name' => 'John Doe Updated', 158 'email' => 'john.updated@example.com' 159]); 160 161// Check the response 162if ($response->isSuccess()) { 163 echo "User updated successfully"; 164} 165``` 166 167### PATCH Request 168 169```php 170// PATCH request to partially update a resource 171$response = patch('https://api.example.com/users/1', [ 172 'email' => 'new.email@example.com' 173]); 174 175// Check the response 176if ($response->isSuccess()) { 177 echo "User email updated successfully"; 178} 179``` 180 181### DELETE Request 182 183```php 184// DELETE request to remove a resource 185$response = delete('https://api.example.com/users/1'); 186 187// Delete with request body (for batch deletions) 188$response = delete('https://api.example.com/users', [ 189 'ids' => [1, 2, 3] 190]); 191 192// Check if the resource was deleted 193if ($response->isSuccess()) { 194 echo "Resource deleted successfully"; 195} 196``` 197 198## Internal Implementation 199 200Internally, these helper functions use the `request_method()` function, which in turn calls the `fetch()` function with the appropriate HTTP method and data configuration: 201 202```php 203function request_method( 204 string $method, 205 string $url, 206 mixed $data = null, 207 ?array $options = [], 208 bool $dataIsQuery = false 209): ResponseInterface 210{ 211 $options = $options ?? []; 212 $options['method'] = $method; 213 214 if ($data !== null) { 215 if ($dataIsQuery) { 216 $options['query'] = $data; 217 } elseif (is_array($data)) { 218 $options['json'] = $data; // Treat arrays as JSON by default 219 } else { 220 $options['body'] = $data; 221 } 222 } 223 224 return fetch($url, $options); 225} 226``` 227 228## Notes 229 230- These helpers provide a more concise way to make common HTTP requests compared to using `fetch()` directly 231- When passing an array as the data parameter in `post()`, `put()`, `patch()`, or `delete()`, it's automatically encoded as JSON 232- For GET requests, the data parameter is treated as query parameters 233- You can still use the full range of request options by passing them in the `$options` parameter 234- All helper functions use the global client instance from `fetch_client()` internally, so any global configuration applies 235 236## See Also 237 238- [fetch()](/api/fetch) - Main function for making HTTP requests 239- [fetch_client()](/api/fetch-client) - Get or configure the global client instance 240- [Client](/api/client) - More details on the Client class 241- [Response](/api/response) - API for working with response objects