diff --git a/composer.json b/composer.json index 76b263e..95def7b 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "require": { "php": ">=5.3", "ext-curl": "*", + "ext-gd": "*", "ext-mbstring": "*" }, "require-dev": { diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 7ae3918..afd2c7b 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -112,21 +112,7 @@ class Curl } $this->curl = curl_init(); - $this->id = uniqid('', true); - $this->setDefaultUserAgent(); - $this->setDefaultTimeout(); - $this->setOpt(CURLINFO_HEADER_OUT, true); - - // Create a placeholder to temporarily store the header callback data. - $header_callback_data = new \stdClass(); - $header_callback_data->rawResponseHeaders = ''; - $header_callback_data->responseCookies = array(); - $this->headerCallbackData = $header_callback_data; - $this->setOpt(CURLOPT_HEADERFUNCTION, createHeaderCallback($header_callback_data)); - - $this->setOpt(CURLOPT_RETURNTRANSFER, true); - $this->headers = new CaseInsensitiveArray(); - $this->setUrl($base_url); + $this->initialize($base_url); } /** @@ -1206,6 +1192,22 @@ class Curl $this->setOpt(CURLOPT_STDERR, $output); } + /** + * Reset + * + * @access public + */ + public function reset() + { + if (function_exists('curl_reset') && is_resource($this->curl)) { + curl_reset($this->curl); + } else { + $this->curl = curl_init(); + } + + $this->initialize(); + } + /** * Destruct * @@ -1485,6 +1487,32 @@ class Curl $this->cookies[implode('', $name_chars)] = implode('', $value_chars); } + + /** + * Initialize + * + * @access private + * @param $base_url + * @throws \ErrorException + */ + private function initialize($base_url = null) + { + $this->id = uniqid('', true); + $this->setDefaultUserAgent(); + $this->setDefaultTimeout(); + $this->setOpt(CURLINFO_HEADER_OUT, true); + + // Create a placeholder to temporarily store the header callback data. + $header_callback_data = new \stdClass(); + $header_callback_data->rawResponseHeaders = ''; + $header_callback_data->responseCookies = array(); + $this->headerCallbackData = $header_callback_data; + $this->setOpt(CURLOPT_HEADERFUNCTION, createHeaderCallback($header_callback_data)); + + $this->setOpt(CURLOPT_RETURNTRANSFER, true); + $this->headers = new CaseInsensitiveArray(); + $this->setUrl($base_url); + } } /** diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 92465a0..587a896 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -3639,4 +3639,22 @@ class CurlTest extends \PHPUnit\Framework\TestCase ); } } + + public function testReset() + { + $php_version = preg_replace('/([\.\+\?\*\(\)\[\]\^\$\/])/', '\\\\\1', 'PHP/' . PHP_VERSION); + + $test = new Test(); + + $user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT')); + $this->assertRegExp('/' . $php_version . '/', $user_agent); + + $test->curl->setUserAgent('New agent'); + $user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT')); + $this->assertEquals('New agent', $user_agent); + + $test->curl->reset(); + $user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT')); + $this->assertRegExp('/' . $php_version . '/', $user_agent); + } }