URI templates in Guzzle 5?

2019-06-04 19:56发布

问题:

Guzzle 3 had URI templates that allowed for a request definition such as

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

In my case, i am looking to exploit the 'segments', but Guzzle 5 doesn't seem to define this.

Instead the closest ive come across was

 *     $client = new Client([
 *         'base_url' => [
 *              'http://www.foo.com/{version}/',
 *              ['version' => '123']
 *          ],
 *         'defaults' => [
 *             'timeout'         => 10,
 *             'allow_redirects' => false,
 *             'proxy'           => '192.168.16.1:10'
 *         ]
 *     ]);

But, this as you see applies to the base_url

Is there anyway i can use a URI template like the one in Guzzle 3?

回答1:

You might have found the answer since you created the post, but here is how to use URI templates with Guzzle 5 (with a simpler example):

$client = new Client([
    'base_url' => 'https://api.github.com',
]);

$response = $client->get(
    ['/users/{username}', ['username' => $username]], // URI template & Parameters
    ['future' => true] // Options
);


标签: php uri guzzle