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"; ?>
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); }
Czechnology Mar 07 2018-11-11T00: 00Z
source share