Curl POST format for CURLOPT_POSTFIELDS

When I use curl via POST and set CURLOPT_POSTFIELD , do I need urlencode or some special format?

for example: If I want to publish 2 fields, the first and last:

 first=John&last=Smith 

What is the exact code / format to be used with curl?

 $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $reply=curl_exec($ch); curl_close($ch); 
+69
post php curl
Mar 07 '11 at 20:20
source share
6 answers

If you send a string, urlencode () is this. Otherwise, if the array is, it must have the value key =>, and the Content-type header is automatically set to multipart/form-data .

In addition, you do not need to create additional functions to build a query for your arrays, you already have this:

 $query = http_build_query($data, '', '&'); 
+76
Mar 07 '11 at 20:35
source share

EDIT : from php5 up we recommend using http_build_query :

 string http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] ) 

A simple example from the manual:

 <?php $data = array('foo'=>'bar', 'baz'=>'boom', 'cow'=>'milk', 'php'=>'hypertext processor'); echo http_build_query($data) . "\n"; /* output: foo=bar&baz=boom&cow=milk&php=hypertext+processor */ ?> 



before php5:

From manual :

CURLOPT_POSTFIELDS

Full details for publishing in "POST" HTTP mode. To publish the file, add the file name with @ and use the full path. A file type can be explicitly specified by following a file name with a type in the format '; type = mimetype '. This parameter can be passed as a string with urlencoded, as 'para1 = val1 & para2 = val2 & ...' or as an array with the field name as the key and field data as the value. If the value is an array, the Content-Type header will be set to multipart / form-data. As with PHP 5.2.0, the files passed to this parameter with the @ prefix must be in the form of an array to work.

So something like this should work fine (with parameters passed in an associative array):

 function preparePostFields($array) { $params = array(); foreach ($array as $key => $value) { $params[] = $key . '=' . urlencode($value); } return implode('&', $params); } 
+44
Mar 07 2018-11-11T00:
source share

Do not pass the string at all!

You can pass an array and let php / curl do the dirty work with encoding, etc.

+34
Mar 07 2018-11-11T00:
source share

According to the PHP manual, the data passed to cURL as a string must be copied URLs. See curl_setopt () and search for CURLOPT_POSTFIELDS .

+3
Mar 07 '11 at 20:30
source share

For CURLOPT_POSTFIELDS parameters can be passed as a string with urlencoded, for example para1=val1¶2=val2&.. , or as an array with the field name as the key and field data as the value

Try the following format:

 $data = json_encode(array( "first" => "John", "last" => "Smith" )); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $output = curl_exec($ch); curl_close($ch); 
+2
Mar 10 '16 at 10:50
source share

This answer took me forever to find. I found that all you have to do is concatenate the URL ("?" After the file name and extension) using a URL encoded query string. It doesn't even look like you have to set the POST cURL parameters. See fake example below:

 //create URL $exampleURL = 'http://www.example.com/example.php?'; // create curl resource $ch = curl_init(); // build URL-encoded query string $data = http_build_query( array('first' => 'John', 'last' => 'Smith', '&'); // set url curl_setopt($ch, CURLOPT_URL, $exampleURL . $data); // return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources <br/> curl_close($ch); 

You can also use file_get_contents() :

 // read entire webpage file into a string $output = file_get_contents($exampleURL . $data); 
-four
Jun 22 '13 at 23:07 on
source share



All Articles