URI templates in Guzzle 5?

给你一囗甜甜゛ 提交于 2019-12-23 02:42:19

问题


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
);


来源:https://stackoverflow.com/questions/27259628/uri-templates-in-guzzle-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!