guzzlehttp/guzzle
Official Information
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and integrates well with web services.
1 A simple interface for building query strings, POST requests, streaming uploads, streaming downloads, using HTTP cookies, uploading JSON data, etc.2 You can send synchronous and asynchronous requests using the same interface.3 Handles requests, responses, and streams using the PSR-7 interface. This allows you to use other PSR-7 compatible libraries with Guzzle.4 Supports PSR-18, allowing interoperability between other PSR-18 HTTP clients.5 Abstracts the underlying HTTP transport, allowing you to write code that is independent of the environment and transport; that is, there are no hard dependencies on cURL, PHP streams, sockets, or non-blocking event loops.6 A middleware system allows you to extend and compose client behavior.
In simpler terms
1. Applicable for simple requests like POST, GET, and file uploads/downloads.
2. The same request URL can be used for both synchronous and asynchronous requests.
3. Request and response data conform to the PSR-7 standard, making it easy to interface with other packages that follow the same standard.
4. Supports PSR-18, with the same principle as above.
5. Facilitates writing business logic without worrying about low-level code like cURL.
6. Users can create requests, send requests, parse responses, etc., in a freely combinable manner.
Composer Package
https://packagist.org/packages/guzzlehttp/guzzle
GitHub Address
https://github.com/guzzle/guzzle
Usage
composer require guzzlehttp/guzzle
Requires PHP version 8 or above.
$url = "http://colormind.io/api/";$method = "post";$header = [ 'Content-Type' => 'application/json',];$postdata = [ 'model' => "default",];$postdata_json = json_encode($postdata);$options = [ 'headers' => $header, 'body' => $postdata_json,];$client = new Client();$request = new Request($method, $url, $header, $postdata_json); //use$res = $client->send($request);echo $res->getBody();
{"result":[[36,142,122],[105,143,130],[233,222,164],[226,169,73],[175,81,119]]}
Recommended Content
Free Public APIs
https://github.com/public-apis/public-apis?tab=readme-ov-file