Guzzle and PSR-7

Guzzle utilizes PSR-7 as the HTTP message interface. This allows Guzzle to work with any other library that utilizes PSR-7 message interfaces.

Guzzle is an HTTP client that sends HTTP requests to a server and receives HTTP responses. Both requests and responses are referred to as messages.

Guzzle relies on the guzzlehttp/psr7 Composer package for its message implementation of PSR-7.

You can create a request using the GuzzleHttp\Psr7\Request class:

  1. use GuzzleHttp\Psr7\Request;
  2. $request = new Request('GET', 'http://httpbin.org/get');
  3. // You can provide other optional constructor arguments.
  4. $headers = ['X-Foo' => 'Bar'];
  5. $body = 'hello!';
  6. $request = new Request('PUT', 'http://httpbin.org/put', $headers, $body);

You can create a response using the GuzzleHttp\Psr7\Response class:

  1. use GuzzleHttp\Psr7\Response;
  2. // The constructor requires no arguments.
  3. $response = new Response();
  4. echo $response->getStatusCode(); // 200
  5. echo $response->getProtocolVersion(); // 1.1
  6. // You can supply any number of optional arguments.
  7. $status = 200;
  8. $headers = ['X-Foo' => 'Bar'];
  9. $body = 'hello!';
  10. $protocol = '1.1';
  11. $response = new Response($status, $headers, $body, $protocol);