Monday, December 21, 2009

Use associative array instead of using querystring-style post fields.

We have faced some issues with posting form details using cURL module of php.

A form having file upload option was not posted correctly.
The content type is multi-part/form-data for this form.

The below line of code will set the post fields for the curl session.
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fiels);

The $post_fields can be specified either as querystring parameters or as an associative array.

Initially we had specified the $post_fields as querystring paramter as $post_fields="field1=value1&field2=value2";

But this approach didn't work for the multi-part/form-data forms.

It works fine after changing this code using an associative array as below.

$post_fields = array("field1" => "value1", "field2" => "value2");

More Articles...

No comments:

Search This Blog