php-guzzleHow to make a GET request with a Guzzle client in PHP?
Making a GET request with a Guzzle client in PHP is easy. Here is an example code block:
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
The output of the example code is 200
.
Code explanation
$client = new GuzzleHttp\Client();
- This creates a new Guzzle client.$res = $client->request('GET', 'https://api.github.com/user', [
- This makes a GET request to the specified URL.'auth' => ['user', 'pass']
- This is an optional parameter that allows you to specify authentication credentials.echo $res->getStatusCode();
- This prints out the response status code.
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...