php 如何在 CURLOPT_POSTFIELDS 中包含数组数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5104607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:50:20  来源:igfitidea点击:

How to include array data in CURLOPT_POSTFIELDS?

phparraysformscurl

提问by diggersworld

I'm basically processing a HTML form with PHP and then sending it off elsewhere for storage and processing. However I'm having trouble sending array lists through curl. I need to do it in such a way that when it gets to the receiving server it's as if it has come straight from the input form.

我基本上是用 PHP 处理 HTML 表单,然后将其发送到其他地方进行存储和处理。但是,我无法通过 curl 发送数组列表。我需要这样做,当它到达接收服务器时,就好像它直接来自输入表单。

I don't receive any errors when using the function if I serialize the arrays, however this makes them unreadable by the server, so they need to keep the post format as if they were coming from a HTML form.

如果我序列化数组,在使用该函数时我不会收到任何错误,但是这会使它们无法被服务器读取,因此他们需要保持发布格式,就好像它们来自 HTML 表单一样。

I'm using Kohana but principles of Curl are still the same, here's my code:

我正在使用 Kohana 但 Curl 的原理仍然相同,这是我的代码:

            $path = "/some/process/path";
            $store = "http://www.website.com";

            $url = $store . $path;

            $screenshots = array();
            $screenshots[0] = 'image1.jpg';
            $screenshots[1] = 'image2.jpg';
            $screenshots[2] = 'image3.jpg';

            $videoLinks = array();
            $videoLinks[0] = 'video1.wmv';
            $videoLinks[1] = 'video2.wmv';

            $params = array(
                'id' => '12',
                'field1' => 'field1text',
                'field2' => 'field2text',
                'field3' => 'field3text',
                'screenshots' => $screenshots,
                'videoLinks' => $videoLinks,
            );

            $options = array(
                CURLOPT_HTTPHEADER => array("Accept: application/json"),
                CURLOPT_TIMEOUT => 30,
                CURLOPT_POST => TRUE,
                CURLOPT_POSTFIELDS => $params,
            );

            $data = Remote::get($url, $options);
            $json = json_decode($data);

Cheers.

干杯。

回答by klkl

I just wanted to share an alternative to http_build_query()

我只是想分享 http_build_query() 的替代方案

You can include array inputs with CURLOPT_POSTFIELDS by supplying each subarray item separately.

您可以通过分别提供每个子数组项来使用 CURLOPT_POSTFIELDS 包含数组输入。

Instead of...

代替...

$videoLinks = array();
$videoLinks[0] = 'video1.wmv';
$videoLinks[1] = 'video2.wmv';

$params = array(
    ...
    'videoLinks' => $videoLinks;
    ...
);

... do this ...

... 做这个 ...

$params = array(
    ...
    'videoLinks[0]' => 'video1.wmv';
    'videoLinks[1]' => 'video2.wmv';
    ...
);

回答by Minion

I'm new to cURL and its PHP version, but as far as I know you can do arrays in your option just fine, just don't forget that if you're sending an array of field=>values then you have to set the Content-Type header to multipart/form-data for proper interpretation. That being said, your array for params is formatted wrong. You have that extra comma after your final array value. That might be what's making it bork.

我是 cURL 和它的 PHP 版本的新手,但据我所知,你可以在你的选项中做数组就好了,只是不要忘记,如果你要发送一个字段 => 值的数组,那么你必须设置将 Content-Type 标头添加到 multipart/form-data 以进行正确解释。话虽如此,您的 params 数组格式错误。在最终数组值之后有额外的逗号。这可能就是让它变得无聊的原因。