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

时间:2014-04-18 18:51:54

标签: php arrays curl

如何在php中为curl 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

当我尝试像这样构建数组时,我会覆盖上一项:

$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 个答案:

答案 0 :(得分:1)

您可以尝试:

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

然后:

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