0

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 Answer 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);
Sign up to request clarification or add additional context in comments.

3 Comments

This still overwrites the previous item in the array.
Do you control the incoming post ?
No, posting this to another website.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.