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

时间:2022-06-26 13:21:53

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:

如何在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

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 个解决方案

#1


1  

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

#1


1  

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