From c1f049bc5dcb4db6d8fc7338bdb41d8e2ef9c173 Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Wed, 8 Feb 2012 14:57:54 +1000 Subject: [PATCH] Add support for PUT requests. Fixes #1 --- library/Requests.php | 21 ++++++++++++++--- library/Requests/Transport/cURL.php | 4 ++++ library/Requests/Transport/fsockopen.php | 3 ++- tests/Transport/Base.php | 30 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/library/Requests.php b/library/Requests.php index 2f946fffa..c4c67608c 100755 --- a/library/Requests.php +++ b/library/Requests.php @@ -26,6 +26,13 @@ class Requests { */ const POST = 'POST'; + /** + * PUT method + * + * @var string + */ + const PUT = 'PUT'; + /** * GET method * @@ -180,9 +187,7 @@ public static function head($url, $headers = array(), $options = array()) { } /**#@-*/ - /** - * Send a POST request - * + /**#@+ * @see request() * @param string $url * @param array $headers @@ -190,9 +195,19 @@ public static function head($url, $headers = array(), $options = array()) { * @param array $options * @return Requests_Response */ + /** + * Send a POST request + */ public static function post($url, $headers = array(), $data = array(), $options = array()) { return self::request($url, $headers, $data, self::POST, $options); } + /** + * Send a PUT request + */ + public static function put($url, $headers = array(), $data = array(), $options = array()) { + return self::request($url, $headers, $data, self::PUT, $options); + } + /**#@-*/ /** * Main interface for HTTP requests diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 6cd23cd57..534a7e4a1 100755 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -89,6 +89,10 @@ public function request($url, $headers = array(), $data = array(), $options = ar curl_setopt($this->fp, CURLOPT_POST, true); curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data); break; + case Requests::PUT: + curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data); + break; case Requests::HEAD: curl_setopt($this->fp, CURLOPT_NOBODY, true); break; diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index 7b0553cc5..83fd51409 100755 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -62,6 +62,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar $out = ''; switch ($options['type']) { case Requests::POST: + case Requests::PUT: if (isset($url_parts['path'])) { $path = $url_parts['path']; if (isset($url_parts['query'])) { @@ -71,7 +72,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar else { $path = '/'; } - $out = "POST $path HTTP/1.0\r\n"; + $out = $options['type'] . " $path HTTP/1.0\r\n"; if (is_array($data)) { $request_body = http_build_query($data, null, '&'); } diff --git a/tests/Transport/Base.php b/tests/Transport/Base.php index 469d436aa..85c8b2907 100755 --- a/tests/Transport/Base.php +++ b/tests/Transport/Base.php @@ -108,6 +108,36 @@ public function testPOSTWithArray() { $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']); } + public function testRawPUT() { + $data = 'test'; + $request = Requests::put('http://httpbin.org/put', array(), $data, $this->getOptions()); + $this->assertEquals(200, $request->status_code); + + $result = json_decode($request->body, true); + $this->assertEquals('test', $result['data']); + } + + public function testFormPUT() { + $data = 'test=true&test2=test'; + $request = Requests::put('http://httpbin.org/put', array(), $data, $this->getOptions()); + $this->assertEquals(200, $request->status_code); + + $result = json_decode($request->body, true); + $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']); + } + + public function testPUTWithArray() { + $data = array( + 'test' => 'true', + 'test2' => 'test', + ); + $request = Requests::put('http://httpbin.org/put', array(), $data, $this->getOptions()); + $this->assertEquals(200, $request->status_code); + + $result = json_decode($request->body, true); + $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']); + } + public function testRedirects() { $request = Requests::get('http://httpbin.org/redirect/6', array(), $this->getOptions()); $this->assertEquals(200, $request->status_code);