Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add DELETE method, fixes #3

  • Loading branch information...
commit 329432f2fc0af8feb62cbb1fd249d5d7a1e26bb0 1 parent c1f049b
Ryan McCue authored
14 library/Requests.php
View
@@ -48,6 +48,13 @@ class Requests {
const HEAD = 'HEAD';
/**
+ * DELETE method
+ *
+ * @var string
+ */
+ const DELETE = 'DELETE';
+
+ /**
* Current version of Requests
*
* @var string
@@ -185,6 +192,13 @@ public static function get($url, $headers = array(), $options = array()) {
public static function head($url, $headers = array(), $options = array()) {
return self::request($url, $headers, null, self::HEAD, $options);
}
+
+ /**
+ * Send a DELETE request
+ */
+ public static function delete($url, $headers = array(), $options = array()) {
+ return self::request($url, $headers, null, self::DELETE, $options);
+ }
/**#@-*/
/**#@+
5 library/Requests/Transport/cURL.php
View
@@ -80,7 +80,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar
$options['hooks']->dispatch('curl.before_request', array(&$this->fp));
$headers = Requests::flattern($headers);
- if (($options['type'] === Requests::HEAD || $options['type'] === Requests::GET) & !empty($data)) {
+ if (in_array($options['type'], array(Requests::HEAD, Requests::GET, Requests::DELETE)) & !empty($data)) {
$url = self::format_get($url, $data);
}
@@ -93,6 +93,9 @@ public function request($url, $headers = array(), $data = array(), $options = ar
curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
break;
+ case Requests::DELETE:
+ curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'DELETE');
+ break;
case Requests::HEAD:
curl_setopt($this->fp, CURLOPT_NOBODY, true);
break;
8 library/Requests/Transport/fsockopen.php
View
@@ -87,12 +87,10 @@ public function request($url, $headers = array(), $data = array(), $options = ar
}
break;
case Requests::HEAD:
- $head = self::format_get($url_parts, $data);
- $out = "HEAD $head HTTP/1.0\r\n";
- break;
- default:
+ case Requests::GET:
+ case Requests::DELETE:
$get = self::format_get($url_parts, $data);
- $out = "GET $get HTTP/1.0\r\n";
+ $out = $options['type'] . " $get HTTP/1.0\r\n";
break;
}
$out .= "Host: {$url_parts['host']}\r\n";
22 tests/Transport/Base.php
View
@@ -138,6 +138,28 @@ public function testPUTWithArray() {
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
}
+ public function testDELETE() {
+ $request = Requests::delete('http://httpbin.org/delete', array(), $this->getOptions());
+ $this->assertEquals(200, $request->status_code);
+
+ $result = json_decode($request->body, true);
+ $this->assertEquals('http://httpbin.org/delete', $result['url']);
+ $this->assertEmpty($result['args']);
+ }
+
+ public function testDELETEWithData() {
+ $data = array(
+ 'test' => 'true',
+ 'test2' => 'test',
+ );
+ $request = Requests::request('http://httpbin.org/delete', array(), $data, Requests::DELETE, $this->getOptions());
+ $this->assertEquals(200, $request->status_code);
+
+ $result = json_decode($request->body, true);
+ $this->assertEquals('http://httpbin.org/delete?test=true&test2=test', $result['url']);
+ $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
+ }
+
public function testRedirects() {
$request = Requests::get('http://httpbin.org/redirect/6', array(), $this->getOptions());
$this->assertEquals(200, $request->status_code);
Please sign in to comment.
Something went wrong with that request. Please try again.