Skip to content

Commit fed0f61

Browse files
committed
Fix php-curl-class#51: Prefer application/x-www-form-urlencoded when POSTing
Thanks @karboom for the report.
1 parent aef528a commit fed0f61

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/Curl.class.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ private function postfields($data)
334334
if (is_array_multidim($data)) {
335335
$data = http_build_multi_query($data);
336336
} else {
337+
$binary_data = false;
337338
foreach ($data as $key => $value) {
338339
// Fix "Notice: Array to string conversion" when $value in
339340
// curl_setopt($ch, CURLOPT_POSTFIELDS, $value) is an array
@@ -344,11 +345,18 @@ private function postfields($data)
344345
// file uploading is deprecated. Please use the CURLFile
345346
// class instead".
346347
} elseif (is_string($value) && strpos($value, '@') === 0) {
348+
$binary_data = true;
347349
if (class_exists('CURLFile')) {
348350
$data[$key] = new CURLFile(substr($value, 1));
349351
}
352+
} elseif ($value instanceof CURLFile) {
353+
$binary_data = true;
350354
}
351355
}
356+
357+
if (!$binary_data) {
358+
$data = http_build_query($data);
359+
}
352360
}
353361
}
354362

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,22 +466,54 @@ public function testNestedData()
466466
);
467467
}
468468

469-
public function testPostUrlEncodedContentType()
469+
public function testPostStringUrlEncodedContentType()
470470
{
471471
$test = new Test();
472472
$test->server('server', 'POST', 'foo=bar');
473473
$this->assertEquals($test->curl->request_headers['Content-Type'], 'application/x-www-form-urlencoded');
474474
}
475475

476-
public function testPostFormDataContentType()
476+
public function testPostArrayUrlEncodedContentType()
477477
{
478478
$test = new Test();
479479
$test->server('server', 'POST', array(
480480
'foo' => 'bar',
481481
));
482+
$this->assertEquals($test->curl->request_headers['Content-Type'], 'application/x-www-form-urlencoded');
483+
}
484+
485+
public function testPostFileFormDataContentType()
486+
{
487+
$file_path = get_png();
488+
489+
$test = new Test();
490+
$test->server('server', 'POST', array(
491+
'image' => '@' . $file_path,
492+
));
482493
$this->assertEquals($test->curl->request_headers['Expect'], '100-continue');
483494
preg_match('/^multipart\/form-data; boundary=/', $test->curl->request_headers['Content-Type'], $content_type);
484495
$this->assertTrue(!empty($content_type));
496+
497+
unlink($file_path);
498+
$this->assertFalse(file_exists($file_path));
499+
}
500+
501+
public function testPostCurlFileFormDataContentType()
502+
{
503+
if (class_exists('CURLFile')) {
504+
$file_path = get_png();
505+
506+
$test = new Test();
507+
$test->server('server', 'POST', array(
508+
'image' => new CURLFile($file_path),
509+
));
510+
$this->assertEquals($test->curl->request_headers['Expect'], '100-continue');
511+
preg_match('/^multipart\/form-data; boundary=/', $test->curl->request_headers['Content-Type'], $content_type);
512+
$this->assertTrue(!empty($content_type));
513+
514+
unlink($file_path);
515+
$this->assertFalse(file_exists($file_path));
516+
}
485517
}
486518

487519
public function testJSONResponse()

0 commit comments

Comments
 (0)