【问题标题】:guzzle command post dataguzzle 指挥所数据
【发布时间】:2013-11-11 20:27:26
【问题描述】:

如何通过guzzle服务客户端getCommand函数获取post数据?

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

【问题讨论】:

    标签: php post guzzle


    【解决方案1】:

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

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

    但是Guzzle 中的POST 请求使用application/x-www-form-urlencoded Content-Type 发送

    $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"
     }
    

    【讨论】:

    • 感谢您成为先驱;)
    猜你喜欢
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2020-06-07
    • 2016-05-10
    • 2018-06-22
    • 2010-11-24
    • 2011-01-21
    • 1970-01-01
    相关资源
    最近更新 更多