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 send multiple requests with Guzzle in PHP?
- How to install PHP Guzzle with Composer?
- How to install PHP Guzzle without Composer?
- How to make HTTP request with PHP Guzzle?
- How to stream with PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to add query parameters to a request with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to mock requests with Guzzle in PHP?
See more codes...