2016-09-28 69 views
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

回答

2

我似乎已經解決了我的問題。

我可以簡單地這樣做:

parse_str($request->getUri()->getQuery(), $query); 

,我現在有查詢參數的數組。

歡迎其他解決方案!