食尸鬼的form_params不接受数组

Pet*_*nev 1 php guzzle guzzle6

我正在使用Guzzle 6,并且无法在客户端主体中传递带有form_params的数组

$postFields = [
    form_params => [
        'data[test]' => "TEST",
        'data[whatever]' => "Whatever..."
    ]
];

$client = new GuzzleClient([
        'cookies' => $jar, // The cookie
        'allow_redirects' => true, // Max 5 Redirects
        'base_uri' => $this->navigateUrl, // Base Uri
        'headers' => $this->headers
]);
$response = $client->post('api',[$postFields]);
Run Code Online (Sandbox Code Playgroud)

最终,当我发送请求时,我的数据不见了……但是,如果我在响应中手动添加数据,它就可以正常工作。

$response = $client->post(
    'api',
    [form_params => [
        'data[test]'=>"TEST",
        'data[wht]' => 'Whatever'
    ],
]
// It's working this way...
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,我希望我很清楚。提前致谢。

Sha*_*ley 6

我看到几个问题。首先是您的$postFields数组似乎没有正确格式化的事实,其次是将$ postFields数组包装在另一个数组中。

$options = [
    'debug' => true,
    'form_params' => [
        'test' => 15,
        'id' => 43252435243654352,
        'name' => 'this is a random name',
    ],
    'on_stats' => $someCallableItem,
];
$response = $client->post('api', $options);
Run Code Online (Sandbox Code Playgroud)