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