Allow support for curl_reset()

This commit is contained in:
Reza Sanaie
2018-06-04 10:21:22 -07:00
parent b52e466fa5
commit 9f34c2bf57
3 changed files with 62 additions and 15 deletions
+1
View File
@@ -15,6 +15,7 @@
"require": {
"php": ">=5.3",
"ext-curl": "*",
"ext-gd": "*",
"ext-mbstring": "*"
},
"require-dev": {
+43 -15
View File
@@ -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);
}
}
/**
+18
View File
@@ -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);
}
}