diff --git a/src/Curl.class.php b/src/Curl.class.php index 405bc09..3a2e59d 100644 --- a/src/Curl.class.php +++ b/src/Curl.class.php @@ -334,6 +334,7 @@ class Curl if (is_array_multidim($data)) { $data = http_build_multi_query($data); } else { + $binary_data = false; foreach ($data as $key => $value) { // Fix "Notice: Array to string conversion" when $value in // curl_setopt($ch, CURLOPT_POSTFIELDS, $value) is an array @@ -344,11 +345,18 @@ class Curl // file uploading is deprecated. Please use the CURLFile // class instead". } elseif (is_string($value) && strpos($value, '@') === 0) { + $binary_data = true; if (class_exists('CURLFile')) { $data[$key] = new CURLFile(substr($value, 1)); } + } elseif ($value instanceof CURLFile) { + $binary_data = true; } } + + if (!$binary_data) { + $data = http_build_query($data); + } } } diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index deae35b..a57f1c9 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -466,22 +466,54 @@ class CurlTest extends PHPUnit_Framework_TestCase ); } - public function testPostUrlEncodedContentType() + public function testPostStringUrlEncodedContentType() { $test = new Test(); $test->server('server', 'POST', 'foo=bar'); $this->assertEquals($test->curl->request_headers['Content-Type'], 'application/x-www-form-urlencoded'); } - public function testPostFormDataContentType() + public function testPostArrayUrlEncodedContentType() { $test = new Test(); $test->server('server', 'POST', array( 'foo' => 'bar', )); + $this->assertEquals($test->curl->request_headers['Content-Type'], 'application/x-www-form-urlencoded'); + } + + public function testPostFileFormDataContentType() + { + $file_path = get_png(); + + $test = new Test(); + $test->server('server', 'POST', array( + 'image' => '@' . $file_path, + )); $this->assertEquals($test->curl->request_headers['Expect'], '100-continue'); preg_match('/^multipart\/form-data; boundary=/', $test->curl->request_headers['Content-Type'], $content_type); $this->assertTrue(!empty($content_type)); + + unlink($file_path); + $this->assertFalse(file_exists($file_path)); + } + + public function testPostCurlFileFormDataContentType() + { + if (class_exists('CURLFile')) { + $file_path = get_png(); + + $test = new Test(); + $test->server('server', 'POST', array( + 'image' => new CURLFile($file_path), + )); + $this->assertEquals($test->curl->request_headers['Expect'], '100-continue'); + preg_match('/^multipart\/form-data; boundary=/', $test->curl->request_headers['Content-Type'], $content_type); + $this->assertTrue(!empty($content_type)); + + unlink($file_path); + $this->assertFalse(file_exists($file_path)); + } } public function testJSONResponse()