Skip to content

Commit 8094e2f

Browse files
committed
Merge pull request php-curl-class#235 from petewatts/master
Fix php-curl-class#234 PATCH request method support data body
2 parents 85742bc + 9d03d46 commit 8094e2f

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/Curl/Curl.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ public function patch($url, $data = array())
441441
$data = $url;
442442
$url = $this->baseUrl;
443443
}
444+
445+
if (is_array($data) && empty($data)) {
446+
$this->unsetHeader('Content-Length');
447+
}
448+
444449
$this->setURL($url);
445450
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH');
446451
$this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data));
@@ -463,10 +468,6 @@ public function post($url, $data = array())
463468
$url = $this->baseUrl;
464469
}
465470

466-
if (is_array($data) && empty($data)) {
467-
$this->unsetHeader('Content-Length');
468-
}
469-
470471
$this->setURL($url);
471472
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'POST');
472473
$this->setOpt(CURLOPT_POST, true);

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testBuildPostDataArgSeparator()
8282

8383
public function testUserAgent()
8484
{
85-
$php_version = 'PHP\/' . PHP_VERSION;
85+
$php_version = preg_replace('/([\.\+\?\*\(\)\[\]\^\$\/])/', '\\\\\1', 'PHP/' . PHP_VERSION);
8686
$curl_version = curl_version();
8787
$curl_version = 'curl\/' . $curl_version['version'];
8888

@@ -409,6 +409,19 @@ public function testPatchRequestMethod()
409409
$this->assertEquals('PATCH', $test->server('request_method', 'PATCH'));
410410
}
411411

412+
public function testPatchData()
413+
{
414+
$test = new Test();
415+
$this->assertEquals('key=value', $test->server('patch', 'PATCH', array(
416+
'key' => 'value',
417+
)));
418+
419+
$test = new Test();
420+
$this->assertEquals('{"key":"value"}', $test->server('patch', 'PATCH', json_encode(array(
421+
'key' => 'value',
422+
))));
423+
}
424+
412425
public function testPatchRequestMethodWithMultidimArray()
413426
{
414427
$data = array(

tests/PHPCurlClass/server.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
} elseif ($test === 'put') {
108108
echo $http_raw_post_data;
109109
exit;
110+
} elseif ($test === 'patch') {
111+
echo $http_raw_post_data;
112+
exit;
110113
} elseif ($test === 'post_multidimensional') {
111114
echo $http_raw_post_data;
112115
exit;

0 commit comments

Comments
 (0)