用JSON发布GuzzleHttp帖子

时间:2017-03-29 04:31:50

标签: php guzzle

在guzzle中将json作为multipart表单发送的正确方法是什么?从下面的代码中,应该可以从发布的页面中迭代userComment。例如:userComment[0]应该{'name' => 'Jon', 'comment' => 'jons comment'}userComment[0]['name']应该打印Jon。但它没有用。

$response = $client->request('POST', $url, [
        'multipart' => [
        [
            'name'     => 'action',
            'contents' => 'addComment',
        ],
        [
            'name'     => 'userComment',
            'contents' => ['json' => ['name' => 'Jon', 'jons comment' => 'Please']]
        ],
        [
            'name'     => 'userComment',
            'contents' => '{name: "mike", comment: "mikes comment"}'
        ]
    ]
]);

文档说Contents应该是字符串,因此'contents' => '{name: "mike", comment: "okay"}'会起作用,但我必须$data = json_encode(userComment[1])才能$data['name']mike这看起来不像是正确的做法。任何帮助,谢谢!

0 个答案:

没有答案
相关问题