URI templatesΒΆ

The $uri passed to one of the client's request creational methods or the base URL of a client can utilize URI templates. Guzzle supports the entire URI templates RFC. URI templates add a special syntax to URIs that replace template place holders with user defined variables.

Every request created by a Guzzle HTTP client passes through a URI template so that URI template expressions are automatically expanded:

$client = new Guzzle\Http\Client('', array('a' => 'hi'));
$request = $client->get('/{a}');

Because of URI template expansion, the URL of the above request will become Notice that the template was expanded using configuration variables of the client. You can pass in custom URI template variables by passing the URI of your request as an array where the first index of the array is the URI template and the second index of the array are template variables that are merged into the client's configuration variables.

$request = $client->get(array('/test{?a,b}', array('b' => 'there')));

The URL for this request will become URI templates aren't limited to just simple variable replacements; URI templates can provide an enormous amount of flexibility when creating request URIs.

$request = $client->get(array('{+path}{/segments*}{?query,data*}', array(
    'path'     => '/foo/bar',
    'segments' => array('one', 'two'),
    'query'    => 'test',
    'data'     => array(
        'more' => 'value'

The resulting URL would become

By default, URI template expressions are enclosed in an opening and closing brace (e.g. {var}). If you are working with a web service that actually uses braces (e.g. Solr), then you can specify a custom regular expression to use to match URI template expressions.


You can learn about all of the different features of URI templates by reading the URI templates RFC.

comments powered by Disqus