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 use PHP Guzzle to make a batch request?
- How to update PHP Guzzle?
- How to set a user agent in PHP Guzzle?
- How to stream with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to download a file with Guzzle in PHP?
- How to handle a RequestException with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
- How to get the response body in PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
See more codes...