When I assign an array of data to be POSTed as a cURL option (via CURLOPT_POSTFIELDS), do I need to urlencode that data first or will that be taken care of?
views:
1641answers:
4I can't seem to find any relevant info on curl_setopt
urlencoding of Array values (if an Array is passed).
However if you're using PHP5, you can use the http_build_query function which automatically returns a query string representation of the Array (encoded and all).
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
EDIT Looking at the C implementation of curl_setopt here at line 847, there doesn't seem to be any urlencoding, just simple string conversion.
You don't have to urlencode first. However, it is important to realize that passing an array will make cURL send it as multipart/form-data
, which explains why it is does not need to get urlencoded (by neither you nor cURL), and you need to use an array if you want to upload files. If you http_build_query()
first (and send it as a string) it will be treated as application/x-www-form-urlencoded
.
POST data is not added to the URL (like GET) so you don't need to URLencode it.
One problem with using an array for CURLOPT_POSTFIELDS is that you can't have a name-value pair with an empty value.