2

如何通过 guzzle 服务客户端getCommand功能获取发布数据?

我的 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');,但似乎不起作用。

4

1 回答 1

3

将内容类型设置为application/x-www-form-urlencoded似乎已经成功了,最初我有:

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

但是POST,请求Guzzle是使用application/x-www-form-urlencodedContent-Type发送的

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

或者,您也可以在 json 模式中执行此操作,设置内容类型的参数:

"content-type": {
    "location": "header",
    "default": "application/x-www-form-urlencoded"
 }
于 2013-11-01T11:02:42.263 回答