use GuzzleHttp\Client; $client = new Client(); $response = $client->request('GET', 'https://api.example.com/posts'); echo $response->getStatusCode(); // 200 echo $response->getBody(); // {"posts":[{"id":1,"title":"Hello World!"},{"id":2,"title":"Lorem Ipsum"}]}
use GuzzleHttp\Client; $client = new Client(); $response = $client->request('POST', 'https://api.example.com/posts', [ 'json' => ['title' => 'New post', 'body' => 'Lorem ipsum'] ]); echo $response->getStatusCode(); // 201 echo $response->getBody(); // {"id":3,"title":"New post","body":"Lorem ipsum"}
use GuzzleHttp\Client; use GuzzleHttp\RequestOptions; $client = new Client(); $response = $client->request('POST', 'https://api.example.com/upload', [ RequestOptions::MULTIPART => [ [ 'name' => 'file', 'contents' => fopen('/path/to/file.jpg', 'r'), 'filename' => 'file.jpg' ] ] ]); echo $response->getStatusCode(); // 200 echo $response->getBody(); // {"success":true}In summary, GuzzleHttp is a package library for PHP that provides a simple interface for sending HTTP requests and handling their responses. Its examples include sending GET and POST requests with JSON data and multipart/form-data with a file.