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 keep alive with Guzzle in PHP?
- How to stream with PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to mock requests with Guzzle in PHP?
- How to add query parameters to a request with PHP Guzzle?
- How to log requests with Guzzle in PHP?
- How to make HTTP request with PHP Guzzle?
- How to post form data with PHP Guzzle?
- How to use PHP Guzzle to make a batch request?
See more codes...