$url = 'http://www.example.com/'; $data = array('foo' => '1', 'bar' => '2');
Ordinarily you create the post body using
http_build_query()
. But let's say the form is expecting the form data to be multipart encoded. Now we've got a challenge. First let's create a function to do the multipart encoding:
function multipart_build_query($fields, $boundary){ $retval = ''; foreach($fields as $key => $value){ $retval .= "--$boundary\nContent-Disposition: form-data; name=\"$key\"\n\n$value\n"; } $retval .= "--$boundary--"; return $retval; }
The boundary is a string that separates the fields. It can be any string you want but you should choose something long enough that it won't randomly show up in your data.
$boundary = '--myboundary-xxx'; $body = multipart_build_query($data, $boundary);
Now make your curl post, but remember to set the content type:
$ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary")); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); $response = curl_exec($ch);
Of course, you could have just used PGBrowser to submit the form, and it will automatically detect when it needs to use multipart encoding. But that would be too easy.