Skip to content

Commit

Permalink
Add support for PUT requests. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
rmccue committed Feb 8, 2012
1 parent 104ca8b commit c1f049b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
21 changes: 18 additions & 3 deletions library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class Requests {
*/
const POST = 'POST';

/**
* PUT method
*
* @var string
*/
const PUT = 'PUT';

/**
* GET method
*
Expand Down Expand Up @@ -180,19 +187,27 @@ public static function head($url, $headers = array(), $options = array()) {
}
/**#@-*/

/**
* Send a POST request
*
/**#@+
* @see request()
* @param string $url
* @param array $headers
* @param array $data
* @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
Expand Down
4 changes: 4 additions & 0 deletions library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion library/Requests/Transport/fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -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, '&');
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Transport/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c1f049b

Please sign in to comment.