php-guzzleHow to use PHP Guzzle Client?
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.
// Create a client with a base URI
$client = new GuzzleHttp\Client(['base_uri' => 'http://httpbin.org/']);
// Send a request to http://httpbin.org/get
$response = $client->request('GET', 'get');
echo $response->getStatusCode(); // 200
echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8'
echo $response->getBody(); // '{"type":"url","url":"http:\/\/httpbin.org\/get","args":{},"headers":{"host":"httpbin.org","accept":"*\/*"}}'
Output example
200
application/json; charset=utf8
{"type":"url","url":"http:\/\/httpbin.org\/get","args":{},"headers":{"host":"httpbin.org","accept":"*\/*"}}
Code explanation
$client = new GuzzleHttp\Client(['base_uri' => 'http://httpbin.org/']);
- creates a client with a base URI$response = $client->request('GET', 'get');
- sends a request to http://httpbin.org/getecho $response->getStatusCode();
- prints the status code of the responseecho $response->getHeaderLine('content-type');
- prints the content type of the responseecho $response->getBody();
- prints the body of the response
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...