php-guzzleHow to use PHP Guzzle Client?
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.
// Create a client with a base URI
$client = new GuzzleHttp\Client(['base_uri' => 'http://httpbin.org/']);
// Send a request to http://httpbin.org/get
$response = $client->request('GET', 'get');
echo $response->getStatusCode(); // 200
echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8'
echo $response->getBody(); // '{"type":"url","url":"http:\/\/httpbin.org\/get","args":{},"headers":{"host":"httpbin.org","accept":"*\/*"}}'
Output example
200
application/json; charset=utf8
{"type":"url","url":"http:\/\/httpbin.org\/get","args":{},"headers":{"host":"httpbin.org","accept":"*\/*"}}
Code explanation
$client = new GuzzleHttp\Client(['base_uri' => 'http://httpbin.org/']);- creates a client with a base URI$response = $client->request('GET', 'get');- sends a request to http://httpbin.org/getecho $response->getStatusCode();- prints the status code of the responseecho $response->getHeaderLine('content-type');- prints the content type of the responseecho $response->getBody();- prints the body of the response
Helpful links
More of Php Guzzle
- How to use a proxy with Guzzle in PHP?
- How to install PHP Guzzle without Composer?
- What version of PHP is required for Guzzle?
- How to make HTTP2 request with PHP Guzzle?
- How to convert a Guzzle request to a cURL request in PHP?
- How to add a bearer token in PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to use multipart/form-data with PHP Guzzle?
- How to log requests with Guzzle in PHP?
See more codes...