如何在CURLOPT_POSTFIELDS中包含数组数据? [英] How to include array data in CURLOPT_POSTFIELDS?

查看:900
本文介绍了如何在CURLOPT_POSTFIELDS中包含数组数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我没有收到任何错误,如果我序列化数组,但是这使得它们不可读的服务器,因此他们需要保持post格式,如果他们来自一个HTML表单。

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.

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

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

            $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);

干杯。

推荐答案

CURLOPT_POSTFIELDS => http_build_query($params),

http://php.net/manual/en/function.http-build-query.php

这篇关于如何在CURLOPT_POSTFIELDS中包含数组数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆