1
我使用狂飲6如何獲得查詢參數在狂飲/ PSR7請求
我試圖嘲弄一個客戶端,並使用它,像這樣:
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
$mock_handler = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], 'foo'),
]);
$history = [];
$history_middleware = Middleware::history($history);
$handler_stack = HandlerStack::create($mock_handler);
$handler_stack->push($history_middleware);
$mock_client = new Client(['handler' => $handler_stack]);
// Use mock client in some way
$mock_client->get("http://example.com", [
'query' => [
'bar' => '10',
'hello' => '20'
],
]);
// ------
// get original request using history
$transaction = $history[0];
/** @var Request $request */
$request = $transaction['request'];
// How can I get the query parameters that was used in the request (i.e. bar)
我的問題是如何能我得到GuzzleHttp\Psr7\Request
類中使用的查詢參數?
我設法得到的最接近的是:$request->getUri()->getQuery()
,但這只是返回一個像這樣的字符串:bar=10&hello=20
。