mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 04:01:47 +00:00
Merge pull request #144 from zachborboa/master
Allow setting the base url for requests
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@
|
||||
"name": "Zach Borboa"
|
||||
}
|
||||
],
|
||||
"version": "3.0.3",
|
||||
"version": "3.1.3",
|
||||
"require": {
|
||||
"php": ">=5.3",
|
||||
"ext-curl": "*"
|
||||
|
||||
+51
-8
@@ -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);
|
||||
|
||||
@@ -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) === '?');
|
||||
|
||||
Regular → Executable
Reference in New Issue
Block a user