Requests

Requests are sent from a client to a server. Requests include the method to be applied to a resource, the identifier of the resource, and the protocol version to use.

Request Methods

When creating a request, you are expected to provide the HTTP method you wish to perform. You can specify any method you’d like, including a custom method that might not be part of RFC 7231 (like “MOVE”).

  1. // Create a request using a completely custom HTTP method
  2. $request = new \GuzzleHttp\Psr7\Request('MOVE', 'http://httpbin.org/move');
  3. echo $request->getMethod();
  4. // MOVE

You can create and send a request using methods on a client that map to the HTTP method you wish to use.

GET

$client->get('http://httpbin.org/get', [/** options **/])

POST

$client->post('http://httpbin.org/post', [/** options **/])

HEAD

$client->head('http://httpbin.org/get', [/** options **/])

PUT

$client->put('http://httpbin.org/put', [/** options **/])

DELETE

$client->delete('http://httpbin.org/delete', [/** options **/])

OPTIONS

$client->options('http://httpbin.org/get', [/** options **/])

PATCH

$client->patch('http://httpbin.org/put', [/** options **/])

For example:

  1. $response = $client->patch('http://httpbin.org/patch', ['body' => 'content']);

Request URI

The request URI is represented by a Psr\Http\Message\UriInterface object. Guzzle provides an implementation of this interface using the GuzzleHttp\Psr7\Uri class.

When creating a request, you can provide the URI as a string or an instance of Psr\Http\Message\UriInterface.

  1. $response = $client->request('GET', 'http://httpbin.org/get?q=foo');

Scheme

The scheme of a request specifies the protocol to use when sending the request. When using Guzzle, the scheme can be set to “http” or “https”.

  1. $request = new Request('GET', 'http://httpbin.org');
  2. echo $request->getUri()->getScheme(); // http
  3. echo $request->getUri(); // http://httpbin.org

Host

The host is accessible using the URI owned by the request or by accessing the Host header.

  1. $request = new Request('GET', 'http://httpbin.org');
  2. echo $request->getUri()->getHost(); // httpbin.org
  3. echo $request->getHeader('Host'); // httpbin.org

Port

No port is necessary when using the “http” or “https” schemes.

  1. $request = new Request('GET', 'http://httpbin.org:8080');
  2. echo $request->getUri()->getPort(); // 8080
  3. echo $request->getUri(); // http://httpbin.org:8080

Path

The path of a request is accessible via the URI object.

  1. $request = new Request('GET', 'http://httpbin.org/get');
  2. echo $request->getUri()->getPath(); // /get

The contents of the path will be automatically filtered to ensure that only allowed characters are present in the path. Any characters that are not allowed in the path will be percent-encoded according to RFC 3986 section 3.3

Query string

The query string of a request can be accessed using the getQuery() of the URI object owned by the request.

  1. $request = new Request('GET', 'http://httpbin.org/?foo=bar');
  2. echo $request->getUri()->getQuery(); // foo=bar

The contents of the query string will be automatically filtered to ensure that only allowed characters are present in the query string. Any characters that are not allowed in the query string will be percent-encoded according to RFC 3986 section 3.4