tags:

views:

444

answers:

2

when using cURL in php to send data in a POST, you use set the cURL option CURLOPT_POSTFIELDS to the data

here's some example data array('foo'=>'bar','ninja'=>'pirate')

now here's where i'm confused.

half of the example code i've seen do some preprocessing on the array, and encode and smush the data into a properly formatted url string.

turning our data array into: "foo=bar&ninja=pirate" and then setting that string as the data to CURLOPT_POSTFIELDS

but the other half of the examples i've seen just pass the array directly to CURLOPT_POSTFIELDS and thats it.

the confusing part comes in that they both do the job. given that they both work, i dont think either one can be said to be "correct".

the only advantage i can see of doing the string method is that you can set a parameter into the url that doesnt have a value like "foo=bar&awesome&ninja=pirate", and i dont know if you can do that via a associative array, since you cant have a key with out a value (i dont think), but it seems like a waste of memory, since your duping the whole array into a string

A: 

This is a duplicate of

http://stackoverflow.com/questions/28395/passing-post-values-with-curl

Mark Biek
+2  A: 

Both ways are acceptable.

The question is; would you rather write out the code to "smush the data" or create an array and let PHP take care of it?

When in doubt if there's no reason not to choose the easy option, choose the easy option :) In this instance (to my knowledge) there's nothing you can't do by passing the parameters as an array , so pass it as an array.