guzzle命令发布数据

时间:2013-10-31 17:37:15

标签: php post guzzle

如何通过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');但似乎没有效果。

1 个答案:

答案 0 :(得分:3)

将内容类型设置为application/x-www-form-urlencoded似乎已经完成了这个技巧,原来我有:

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

POST中的Guzzle次请求与application/x-www-form-urlencoded内容类型

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

或者您也可以在json架构中执行此操作,设置content-type的参数:

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