2015-03-02 19 views
1

我正在使用Guzzle 3處理超媒體HTTP API,並將URL路徑用作參數存在一些問題。該服務將返回訪問資源的URL路徑,除了參數化路徑正在URLEncoded之外,該服務主要工作。如何防止URLEncoding操作參數中的Guzzle 3?

這裏的工作在服務說明

"getWidget": { 
    "uri": "{path}", 
    "summary": "Get Widget", 
    "httpMethod": "GET", 
    "responseType": "class", 
    "responseClass": "Service\\Responses\\Widget", 
    "parameters": { 
    "path": { 
     "location": "uri" 
    } 
    } 
} 

我執行的操作後的樣品做:

$client = WidgetClient::factory(array('base_url' => 'example.com')); 

$args = array('path' => '/widget/abc123'); 
$command = $client->getCommand('getWidget', $args); 
$result = $command->execute(); 

當執行客戶端的請求:http://example.com/%2Fwidget%2Fabc123而不是http://example.com/widgetabc123

我追蹤了參數處理到UriTemplate::expandMatch(),它執行rawurlencode($variable)調用編碼參數 - 但我看不到清晰的方式來避免編碼。

因此,使用Guzzle 3及其服務描述,如何將URL路徑作爲參數傳遞,而不需要使用URLEncoded?

回答

0

作爲臨時解決方法(我希望有更好的方法),我訂閱了client.create_request事件並修改了刪除編碼的路徑。這是最初的版本,它會在最終版本之前被清除:

public function onClientCreateRequest(Event $event) 
{ 
    /** @var Request $request */ 
    $request = $event['request']; 
    if (empty($request)) { 
     return; 
    } 

    $url = $request->getUrl(true); 
    if (empty($url)) { 
     return; 
    } 

    $path = $url->getPath(); 
    $path = rawurldecode($path); 
    $path = str_replace('//', '/', $path); 

    //$url->setPath($path); 
    $request->setPath($path); 
} 
相關問題