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 make HTTP2 request with PHP Guzzle?
- How to set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to post form data with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to use Monolog with Guzzle in PHP?
- How to send multipart requests with Guzzle in PHP?
- How to make an HTTP POST request with JSON using PHP Guzzle?
- How to use GraphQL with PHP Guzzle?
- How to use multipart/form-data with PHP Guzzle?
See more codes...