Skip to content

Commit e151a8c

Browse files
committed
Merge pull request php-curl-class#176 from zachborboa/master
Fix php-curl-class#171: Allow sending body data in delete()
2 parents 733601f + 009f05c commit e151a8c

File tree

8 files changed

+48
-15
lines changed

8 files changed

+48
-15
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Curl::buildPostData($data)
158158
Curl::call()
159159
Curl::close()
160160
Curl::complete($callback)
161-
Curl::delete($url, $data = array())
161+
Curl::delete($url, $query_parameters = array(), $data = array())
162162
Curl::download($url, $mixed_filename)
163163
Curl::downloadComplete($fh)
164164
Curl::error($callback)
@@ -196,7 +196,7 @@ Curl::is_array_assoc($array)
196196
Curl::is_array_multidim($array)
197197
MultiCurl::__construct($base_url = null)
198198
MultiCurl::__destruct()
199-
MultiCurl::addDelete($url, $data = array())
199+
MultiCurl::addDelete($url, $query_parameters = array(), $data = array())
200200
MultiCurl::addDownload($url, $mixed_filename)
201201
MultiCurl::addGet($url, $data = array())
202202
MultiCurl::addHead($url, $data = array())

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"name": "Zach Borboa"
1313
}
1414
],
15-
"version": "3.4.5",
15+
"version": "3.5.5",
1616
"require": {
1717
"php": ">=5.3",
1818
"ext-curl": "*"

src/Curl/Curl.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Curl
66
{
7-
const VERSION = '3.4.5';
7+
const VERSION = '3.5.5';
88
const DEFAULT_TIMEOUT = 30;
99

1010
public $curl;
@@ -146,15 +146,17 @@ public function progress($callback)
146146
$this->setOpt(CURLOPT_NOPROGRESS, false);
147147
}
148148

149-
public function delete($url, $data = array())
149+
public function delete($url, $query_parameters = array(), $data = array())
150150
{
151151
if (is_array($url)) {
152-
$data = $url;
152+
$data = $query_parameters;
153+
$query_parameters = $url;
153154
$url = $this->base_url;
154155
}
155-
$this->setURL($url, $data);
156-
$this->unsetHeader('Content-Length');
156+
157+
$this->setURL($url, $query_parameters);
157158
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
159+
$this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data));
158160
return $this->exec();
159161
}
160162

src/Curl/MultiCurl.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ public function __construct($base_url = null)
2727
$this->setURL($base_url);
2828
}
2929

30-
public function addDelete($url, $data = array())
30+
public function addDelete($url, $query_parameters = array(), $data = array())
3131
{
3232
if (is_array($url)) {
33-
$data = $url;
33+
$data = $query_parameters;
34+
$query_parameters = $url;
3435
$url = $this->base_url;
3536
}
3637
$curl = new Curl();
37-
$curl->setURL($url, $data);
38-
$curl->unsetHeader('Content-Length');
38+
$curl->setURL($url, $query_parameters);
3939
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
40+
$curl->setOpt(CURLOPT_POSTFIELDS, $curl->buildPostData($data));
4041
$this->addHandle($curl);
4142
return $curl;
4243
}

tests/PHPCurlClass/Helper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ public function __construct()
1515
$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
1616
}
1717

18-
public function server($test, $request_method, $data = array())
18+
public function server($test, $request_method, $query_parameters = array(), $data = array())
1919
{
2020
$this->curl->setHeader('X-DEBUG-TEST', $test);
2121
$request_method = strtolower($request_method);
22-
$this->curl->$request_method(self::TEST_URL, $data);
22+
if (is_array($data) && empty($data)) {
23+
$this->curl->$request_method(self::TEST_URL, $query_parameters);
24+
} else {
25+
$this->curl->$request_method(self::TEST_URL, $query_parameters, $data);
26+
}
2327
return $this->curl->response;
2428
}
2529
}

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function testUrl()
128128
$this->assertEquals(Test::TEST_URL, $test->curl->base_url);
129129
$this->assertEquals(Test::TEST_URL, $test->curl->url);
130130

131-
// curl -v --get --request DELETE "http://127.0.0.1:8000/" --data "foo=bar"
131+
// curl -v --request DELETE "http://127.0.0.1:8000/?foo=bar"
132132
$test = new Test();
133133
$test->server('server', 'DELETE', $data);
134134
$this->assertEquals(Test::TEST_URL, $test->curl->base_url);
@@ -151,6 +151,12 @@ public function testSetUrlInConstructor()
151151
{
152152
$data = array('key' => 'value');
153153

154+
$curl = new Curl(Test::TEST_URL);
155+
$curl->setHeader('X-DEBUG-TEST', 'delete_with_body');
156+
$curl->delete($data, array('wibble' => 'wubble'));
157+
$this->assertEquals(Test::TEST_URL, $curl->base_url);
158+
$this->assertEquals('{"get":{"key":"value"},"post":{"wibble":"wubble"}}', $curl->raw_response);
159+
154160
$curl = new Curl(Test::TEST_URL);
155161
$curl->setHeader('X-DEBUG-TEST', 'get');
156162
$curl->delete($data);
@@ -419,6 +425,10 @@ public function testDelete()
419425
'test' => 'delete',
420426
'key' => 'test',
421427
)));
428+
429+
$test = new Test();
430+
$test->server('delete_with_body', 'DELETE', array('foo' => 'bar'), array('wibble' => 'wubble'));
431+
$this->assertEquals('{"get":{"foo":"bar"},"post":{"wibble":"wubble"}}', $test->curl->raw_response);
422432
}
423433

424434
public function testHeadRequestMethod()

tests/PHPCurlClass/PHPMultiCurlClassTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,15 @@ public function testSetUrlInConstructor()
17311731
{
17321732
$data = array('key' => 'value');
17331733

1734+
$multi_curl = new MultiCurl(Test::TEST_URL);
1735+
$multi_curl->setHeader('X-DEBUG-TEST', 'delete_with_body');
1736+
$multi_curl->addDelete($data, array('wibble' => 'wubble'))->complete(function($instance) {
1737+
PHPUnit_Framework_Assert::assertEquals(Test::TEST_URL, $instance->base_url);
1738+
PHPUnit_Framework_Assert::assertEquals('{"get":{"key":"value"},"post":{"wibble":"wubble"}}',
1739+
$instance->raw_response);
1740+
});
1741+
$multi_curl->start();
1742+
17341743
$multi_curl = new MultiCurl(Test::TEST_URL);
17351744
$multi_curl->setHeader('X-DEBUG-TEST', 'get');
17361745
$multi_curl->addDelete($data)->complete(function($instance) {

tests/PHPCurlClass/server.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@
223223

224224
echo 'OK';
225225
exit;
226+
} elseif ($test === 'delete_with_body') {
227+
header('Content-Type: application/json');
228+
echo json_encode(array(
229+
'get' => $_GET,
230+
'post' => $_POST,
231+
));
232+
exit;
226233
}
227234

228235
header('Content-Type: text/plain');

0 commit comments

Comments
 (0)