Use http_build_multi_query() to build POST data string containing multiple values for the same key

This commit is contained in:
php-curl-class
2013-08-24 13:04:02 -07:00
parent bc96a04df4
commit dc9bb513ec
3 changed files with 29 additions and 3 deletions
+26 -1
View File
@@ -23,7 +23,7 @@ class Curl {
function post($url, $data=array()) {
$this->setopt(CURLOPT_URL, $url);
$this->setopt(CURLOPT_POST, TRUE);
$this->setopt(CURLOPT_POSTFIELDS, $data);
$this->setopt(CURLOPT_POSTFIELDS, $this->_postfields($data));
$this->_exec();
}
@@ -81,6 +81,31 @@ class Curl {
curl_close($this->curl);
}
function http_build_multi_query($data, $key=NULL) {
$query = array();
foreach ($data as $k => $value) {
if (is_string($value)) {
$query[] = urlencode(is_null($key) ? $k : $key) . '=' . urlencode($value);
}
else if (is_array($value)) {
$query[] = $this->http_build_multi_query($value, $k . '[]');
}
}
return implode('&', $query);
}
function _postfields($data) {
$multidimensional = !(count($data) === count($data, COUNT_RECURSIVE));
if ($multidimensional) {
$data = $this->http_build_multi_query($data);
}
return $data;
}
function _exec() {
$this->response = curl_exec($this->curl);
$this->curl_error_code = curl_errno($this->curl);
+1 -1
View File
@@ -53,7 +53,7 @@ class CurlTest extends PHPUnit_Framework_TestCase {
'wubble',
'wobble',
),
)) === '{"test":"post_multidimensional","key":"file","file":["wibble","wubble","wobble"]}');
)) === 'test=post_multidimensional&key=file&file%5B%5D=wibble&file%5B%5D=wubble&file%5B%5D=wobble');
}
public function testPostFilePathUpload() {
+2 -1
View File
@@ -20,7 +20,8 @@ if ($test == 'http_basic_auth') {
exit;
}
else if ($test === 'post_multidimensional') {
echo json_encode($_POST);
$http_raw_post_data = file_get_contents('php://input');
echo $http_raw_post_data;
exit;
}
else if ($test === 'post_file_path_upload') {