2015-06-20 35 views
1

升級到Guzzle6後,我無法弄清楚如何爲客戶端設置默認查詢字符串。在Guzzle 6中設置默認查詢字符串

我有以下幾點:

$client = new \GuzzleHttp\Client([ 
    'base_uri' => 'http://api.example.org/', 
    'query' => ['key' => 'secretKey'] 
]); 
$client->get('extract', ['query' => ['url' => $url]]); 

在此請求我的默認查詢刺痛key=secretKey被忽略。

我該如何讓它工作?

回答

-1

你可以嘗試讓使用「getConfig」方法默認「查詢」選項,然後用新的「查詢」選項,這裏的例子將它們合併:

$client = new GuzzleHttp\Client([ 
    'base_uri' => 'http://api.example.org', 
    'query' => ['key' => 'secretKey'] 
]); 

然後你就可以輕鬆發送GET請求:

$client->get('/extract', [ 
    'query' => array_merge(
     $client->getConfig('query'), 
     ['url' => $url] 
    ) 
]); 

其他信息,你可以在這裏找到Request Options

0

狂飲V6不支持客戶端選項中默認的查詢字符串。中間件是必需的。

$handler = new HandlerStack(); 
$handler->setHandler(new CurlHandler()); 

//Add access token 
$handler->unshift(Middleware::mapRequest(function(RequestInterface $request) { 
    return $request->withUri(Uri::withQueryValue($request->getUri(), 'key', 'value')); 
})); 

//Create client 
$this->client = new Client([ 
    'base_uri' => '' 
    'handler' => $handler 
]); 

查看https://github.com/guzzle/guzzle/issues/1138的來源。