简体   繁体   English

Guzzle 6 array_merge 不能通过表单参数工作?

[英]Guzzle 6 array_merge not working by form params?

I am running guzzlehttp/guzzle version 6.5.5.我正在运行guzzlehttp/guzzle版本6.5.5. on PHP 7.3.8 and trying to make a post request.PHP 7.3.8上并尝试发出发布请求。 One of the required fields is blog .必填字段之一是blog

So when I do this:所以当我这样做时:

$comment = [
    'foo' => 'bar'
];
$data = $comment + ["blog" => $this->blogUrl];
dump($data);
$options = [
    'form_params' => [
        $data,
    ],
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ]
];
$response = $this->client->post($url, $options);

I get the error that the "blog" fields is missing.我收到“博客”字段丢失的错误。

The result of the above dump is:上述转储的结果是:

array:2 [
  "foo" => "bar"
  "blog" => "http://jigal.dev"
]

But when I do this (notice that I moved 'blog' into the form_params key):但是当我这样做时(注意我将“博客”移动到 form_params 键中):

$comment = [
    'foo' => 'bar'
];
$data = $comment;
dump($data);
$options = [
    'form_params' => [
        'blog' => $this->blogUrl,
        $data,
    ],
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ]
];
$response = $this->client->post($url, $options);

The error is gone.错误消失了。

Doing array_merge($comment, ["blog" => this->blogUrl] in stead of $data = $comment + ["blog" => $this->blogUrl]; does not make a difference.array_merge($comment, ["blog" => this->blogUrl]而不是$data = $comment + ["blog" => $this->blogUrl];并没有什么不同。

Any suggestions?有什么建议么?

This code is working:此代码有效:

$data = ["blog" => $this->blogUrl] + $comment;
'form_params' => $data

Instead of form_params => [ $data ]而不是form_params => [ $data ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM