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 use PHP Guzzle to make a batch request?
- How to set a user agent in PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- How to send multipart requests with Guzzle in PHP?
- How to make HTTP request with PHP Guzzle?
- How to post form data with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to log requests with Guzzle in PHP?
- How to use multipart/form-data with PHP Guzzle?
- How to send multiple requests with Guzzle in PHP?
See more codes...