2013-10-31 64 views
2

如何通過guzzle服務客戶端getCommand函數獲取發佈數據?guzzle命令發佈數據

我的JSON如下所示:

"createMessage": { 
     "httpMethod": "POST", 
     "uri": "conversations/{conversation_id}/message", 
     "summary": "conversations by user", 
     "responseType": "class", 
     "responseClass": "\\Conversations\\Message", 
     "parameters": { 
      "conversation_id": { 
       "location": "uri", 
       "description": "conversation id", 
       "type": "integer" 
      }, 
      "message": { 
       "location": "postField", 
       "sentAs": "message", 
       "type": "string" 
      } 
     } 
    } 

然後我現在把我的文章數據,通過getCommand通過陣列的一部分:

$client = new \Guzzle\Service\Client(); 
$client->setDescription(\Guzzle\Service\Description\ServiceDescription::factory(__DIR__ . '/client.json')); 
$command = $client->getCommand('createMessage', array('conversation_id' => 6, 'message' => 'test post message')); 

它創造了新的記錄在數據庫中但發佈數據爲空,因此'message'字段留空。

我已嘗試設置$client->setPostField('message', 'test post message');但似乎不起作用。

回答

3

設置內容類型application/x-www-form-urlencoded似乎做的伎倆,我原本:

$command->set('command.headers', array('content-type' => 'application/json')); 

但是在GuzzlePOST請求與application/x-www-form-urlencoded的Content-Type

$command->set('command.headers', array('content-type' => 'application/x-www-form-urlencoded')); 

或者你發送也可以在json schema中做到這一點,設置內容類型的參數:

"content-type": { 
    "location": "header", 
    "default": "application/x-www-form-urlencoded" 
} 
+0

感謝您成爲先鋒;) –