diff --git a/composer.json b/composer.json index 7e7f3dd..c61438f 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "name": "Zach Borboa" } ], - "version": "3.0.3", + "version": "3.1.3", "require": { "php": ">=5.3", "ext-curl": "*" diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index c6d3220..9f34d7c 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -4,7 +4,7 @@ namespace Curl; class Curl { - const VERSION = '3.0.3'; + const VERSION = '3.1.3'; const DEFAULT_TIMEOUT = 30; public $curl; @@ -135,8 +135,14 @@ class Curl $this->complete_function = $callback; } - public function delete($url, $data = array()) + public function delete($url_mixed, $data = array()) { + if (is_array($url_mixed)) { + $url = $this->base_url; + $data = $url_mixed; + } else { + $url = $url_mixed; + } $this->setURL($url, $data); $this->unsetHeader('Content-Length'); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE'); @@ -217,8 +223,14 @@ class Curl return $this->response; } - public function get($url, $data = array()) + public function get($url_mixed, $data = array()) { + if (is_array($url_mixed)) { + $url = $this->base_url; + $data = $url_mixed; + } else { + $url = $url_mixed; + } $this->setURL($url, $data); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'GET'); $this->setOpt(CURLOPT_HTTPGET, true); @@ -230,8 +242,14 @@ class Curl return $this->options[$option]; } - public function head($url, $data = array()) + public function head($url_mixed, $data = array()) { + if (is_array($url_mixed)) { + $url = $this->base_url; + $data = $url_mixed; + } else { + $url = $url_mixed; + } $this->setURL($url, $data); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'HEAD'); $this->setOpt(CURLOPT_NOBODY, true); @@ -244,16 +262,28 @@ class Curl return strlen($header); } - public function options($url, $data = array()) + public function options($url_mixed, $data = array()) { + if (is_array($url_mixed)) { + $url = $this->base_url; + $data = $url_mixed; + } else { + $url = $url_mixed; + } $this->setURL($url, $data); $this->unsetHeader('Content-Length'); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS'); return $this->exec(); } - public function patch($url, $data = array()) + public function patch($url_mixed, $data = array()) { + if (is_array($url_mixed)) { + $url = $this->base_url; + $data = $url_mixed; + } else { + $url = $url_mixed; + } $this->setURL($url); $this->unsetHeader('Content-Length'); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH'); @@ -261,8 +291,15 @@ class Curl return $this->exec(); } - public function post($url, $data = array()) + public function post($url_mixed, $data = array()) { + if (is_array($url_mixed)) { + $url = $this->base_url; + $data = $url_mixed; + } else { + $url = $url_mixed; + } + if (is_array($data) && empty($data)) { $this->unsetHeader('Content-Length'); } @@ -274,8 +311,14 @@ class Curl return $this->exec(); } - public function put($url, $data = array()) + public function put($url_mixed, $data = array()) { + if (is_array($url_mixed)) { + $url = $this->base_url; + $data = $url_mixed; + } else { + $url = $url_mixed; + } $this->setURL($url); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'PUT'); $put_data = $this->buildPostData($data); diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 3965315..9ff0efb 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -131,6 +131,60 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals(Test::TEST_URL . '?' . http_build_query($data), $test->curl->url); } + public function testSetUrl() + { + $data = array('key' => 'value'); + + $curl = new Curl(); + $curl->setHeader('X-DEBUG-TEST', 'get'); + $curl->setUrl(Test::TEST_URL); + $curl->delete($data); + $this->assertEquals(Test::TEST_URL, $curl->base_url); + $this->assertEquals('key=value', $curl->response); + + $curl = new Curl(); + $curl->setHeader('X-DEBUG-TEST', 'get'); + $curl->setUrl(Test::TEST_URL); + $curl->get($data); + $this->assertEquals(Test::TEST_URL, $curl->base_url); + $this->assertEquals('key=value', $curl->response); + + $curl = new Curl(); + $curl->setHeader('X-DEBUG-TEST', 'get'); + $curl->setUrl(Test::TEST_URL); + $curl->head($data); + $this->assertEquals(Test::TEST_URL, $curl->base_url); + $this->assertEquals('HEAD /?key=value HTTP/1.1', $curl->request_headers['Request-Line']); + + $curl = new Curl(); + $curl->setHeader('X-DEBUG-TEST', 'get'); + $curl->setUrl(Test::TEST_URL); + $curl->options($data); + $this->assertEquals(Test::TEST_URL, $curl->base_url); + $this->assertEquals('key=value', $curl->response); + + $curl = new Curl(); + $curl->setHeader('X-DEBUG-TEST', 'request_method'); + $curl->setUrl(Test::TEST_URL); + $curl->patch($data); + $this->assertEquals(Test::TEST_URL, $curl->base_url); + $this->assertEquals('PATCH', $curl->response); + + $curl = new Curl(); + $curl->setHeader('X-DEBUG-TEST', 'post'); + $curl->setUrl(Test::TEST_URL); + $curl->post($data); + $this->assertEquals(Test::TEST_URL, $curl->base_url); + $this->assertEquals('key=value', $curl->response); + + $curl = new Curl(); + $curl->setHeader('X-DEBUG-TEST', 'put'); + $curl->setUrl(Test::TEST_URL); + $curl->put($data); + $this->assertEquals(Test::TEST_URL, $curl->base_url); + $this->assertEquals('key=value', $curl->response); + } + public function testPostRequestMethod() { $test = new Test(); @@ -618,7 +672,7 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals('OK', $test->curl->response); } - public function testRequestURL() + public function testRequestUrl() { $test = new Test(); $this->assertFalse(substr($test->server('request_uri', 'GET'), -1) === '?'); diff --git a/tests/before_script.sh b/tests/before_script.sh old mode 100644 new mode 100755 diff --git a/tests/syntax.sh b/tests/check_syntax.sh similarity index 100% rename from tests/syntax.sh rename to tests/check_syntax.sh