21

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?

5 Answers 5

33

The C implementation of curl_setopt doesn't seem to URL-encode the text. However, in PHP5, the http_build_query function returns a query string representation of the array that is URL-encoded.

Example Usage

  $curl_parameters = array(
    'param1' => $param1,
    'param2' => $param2
  );

  $curl_options = array(
    CURLOPT_URL => "http://localhost/service",
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query( $curl_parameters ),
    CURLOPT_HTTP_VERSION => 1.0,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false
  );

  $curl = curl_init();
  curl_setopt_array( $curl, $curl_options );
  $result = curl_exec( $curl );

  curl_close( $curl );
Sign up to request clarification or add additional context in comments.

2 Comments

It only says anything about the urlencoded part if your passing a string of data, nothing about if you need to preencode an array.
Be careful with http_build_query(), when you post a file it'll crumble the '@' sign.
14

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.

4 Comments

It does not urlencode them, there is no need to since it is getting sent as multipart form.
I know that, but the spec doesn't say anything about generating an multipart/form-data if an Array is passed.
That's right, the documentation is slacky. I had to figure it out by myself by sniffing the packets and I think it's also somewhere in the comments (@the PHP doc). Since you have to pass an array if you want to upload files, multipart is the only way anyway.
Despite being an old topic I found this answer helpful. The endpoint I was connecting to only allowed application/x-www-form-urlencoded so I was setting it manually using the headers but then found this answer. Much preferred. PHP.net added a notice for this but being below the examples lots of people don't see it. php.net/manual/en/…
1

One problem with using an array for CURLOPT_POSTFIELDS is that you can't have a name-value pair with an empty value.

1 Comment

I have ran into the same issue, where I have an array which can sometimes by empty, so for when it's empty, it doesn't send the key for that array at all.
1

I use:

curl_setopt($curl , CURLOPT_POSTFIELDS, $array );

instead of:

curl_setopt($curl , CURLOPT_POSTFIELDS, http_build_query($array)  );

Comments

-1

POST data is not added to the URL (like GET) so you don't need to URLencode it.

1 Comment

wrong, if passed as a string to the curl_setopt function, it needs to be encoded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.