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”).
// Create a request using a completely custom HTTP method
$request = new \GuzzleHttp\Psr7\Request('MOVE', 'http://httpbin.org/move');
echo $request->getMethod();
// 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:
$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
.
$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”.
$request = new Request('GET', 'http://httpbin.org');
echo $request->getUri()->getScheme(); // http
echo $request->getUri(); // http://httpbin.org
Host
The host is accessible using the URI owned by the request or by accessing the Host header.
$request = new Request('GET', 'http://httpbin.org');
echo $request->getUri()->getHost(); // httpbin.org
echo $request->getHeader('Host'); // httpbin.org
Port
No port is necessary when using the “http” or “https” schemes.
$request = new Request('GET', 'http://httpbin.org:8080');
echo $request->getUri()->getPort(); // 8080
echo $request->getUri(); // http://httpbin.org:8080
Path
The path of a request is accessible via the URI object.
$request = new Request('GET', 'http://httpbin.org/get');
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.
$request = new Request('GET', 'http://httpbin.org/?foo=bar');
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