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 use PHP Guzzle to make a batch request?
- How to send multipart requests with Guzzle in PHP?
- How to update PHP Guzzle?
- How to post form data with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
- What version of PHP is required for Guzzle?
- How to use a proxy with Guzzle in PHP?
- PHP Guzzle get request example
- How to use cookies with Guzzle in PHP?
- How to set a user agent in PHP Guzzle?
See more codes...