如何包括CURLOPT_POSTFIELDS阵列中的数据? [英] How to include array data in CURLOPT_POSTFIELDS?

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

问题描述

我基本上使用PHP处理HTML表单,然后发送它关闭其他地方存储和处理。但是我无法通过卷曲发送数组列表。我需要做的是在这样一种方式,当它到达接收服务器就好像它已经从输入表单来直。

我不使用该功能时,如果我的序列化阵列,但是这也使得他们无法读取服务器,所以他们需要不断的帖子格式,如果他们是从一个HTML表单来收到任何错误。

我使用的Kohana但卷曲的原则

仍然是相同的,这是我的code:

  $ PATH =/一些/流程/路径;
            $店=htt​​p://www.website.com;            $ URL = $商店。 $路径;            $截图=阵列();
            $截图[0] ='image1.jpg';
            $截图[1] ='image2.jpg';
            $截图[2] ='image3.jpg';            $ videoLinks =阵列();
            $ videoLinks [0] ='video1.wmv';
            $ videoLinks [1] ='video2.wmv';            $ PARAMS =阵列(
                'ID'=> '12',
                字段1'=> field1text',
                '字段2'=> field2text',
                FIELD3'=> field3text',
                屏幕截图= GT; $截图,
                videoLinks'=> $ videoLinks,
            );            $选项=数组(
                CURLOPT_HTTPHEADER =>阵列(接受:应用/ JSON),
                CURLOPT_TIMEOUT => 30,
                CURLOPT_POST =>真正,
                CURLOPT_POSTFIELDS => $参数,可以
            );            $数据=远程::获取($网址,$选项);
            $ JSON = json_de code($的数据);

干杯。


解决方案

  CURLOPT_POSTFIELDS => http_build_query($ params)方法,

<一个href=\"http://php.net/manual/en/function.http-build-query.php\">http://php.net/manual/en/function.http-build-query.php

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.

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.

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

Cheers.

解决方案

CURLOPT_POSTFIELDS => http_build_query($params),

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

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

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