dousi6192 2014-04-18 18:51
浏览 36

在PHP中为CURL Post构建一个数组

How do you build an array for a curl post in php without overwriting the previous field? The headers show me the following in the post:

Content-Disposition: form-data; name="price[][unit]"

One
-----------------------------122371200014463
Content-Disposition: form-data; name="price[][price]"

50
-----------------------------122371200014463
Content-Disposition: form-data; name="price[][unit]"

Two
-----------------------------122371200014463
Content-Disposition: form-data; name="price[][price]"

95
-----------------------------122371200014463
Content-Disposition: form-data; name="price[][unit]"

Three
-----------------------------122371200014463
Content-Disposition: form-data; name="price[][price]"
140

I keep overwriting the previous item when I try to build the array like this:

$postPrice['price[][unit]']   =  'One';
$postPrice['price[][price]']   =  $one;
$postPrice['price[][unit]']   =  'Two';
$postPrice['price[][price]']   =  $two;
$postPrice['price[][unit]']   =  'Three';
$postPrice['price[][price]']   =  $three;
  • 写回答

1条回答 默认 最新

  • dpzbzp8728 2014-04-18 18:54
    关注

    You can try:

    $fields = array(
                'price[][unit]' => "One",
                'price[][price]' => $one
                )
            );
    $fields_string = http_build_query($fields);
    

    then:

    curl_setopt($ch,CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝