php-guzzlePHP Guzzle usage example
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.
Example code
<?php
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// 200
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// '{"id": 1420053, "name": "michael", "email": "[email protected]"}'
?>
Output example
200
application/json; charset=utf8
{"id": 1420053, "name": "michael", "email": "[email protected]"}
Code explanation
$client = new GuzzleHttp\Client();
- creates a new Guzzle client instance$res = $client->request('GET', 'https://api.github.com/user', [ 'auth' => ['user', 'pass'] ]);
- sends a GET request to the specified URL with authentication credentialsecho $res->getStatusCode();
- prints the response status codeecho $res->getHeader('content-type');
- prints the response headerecho $res->getBody();
- prints the response body
Helpful links
More of Php Guzzle
- How to set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to handle a RequestException with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- What version of PHP is required for Guzzle?
- How to make HTTP request with PHP Guzzle?
- How to stream with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
See more codes...