Skip to content

Commit 9f34c2b

Browse files
author
Reza Sanaie
committed
Allow support for curl_reset()
1 parent b52e466 commit 9f34c2b

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"require": {
1616
"php": ">=5.3",
1717
"ext-curl": "*",
18+
"ext-gd": "*",
1819
"ext-mbstring": "*"
1920
},
2021
"require-dev": {

src/Curl/Curl.php

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,7 @@ public function __construct($base_url = null)
112112
}
113113

114114
$this->curl = curl_init();
115-
$this->id = uniqid('', true);
116-
$this->setDefaultUserAgent();
117-
$this->setDefaultTimeout();
118-
$this->setOpt(CURLINFO_HEADER_OUT, true);
119-
120-
// Create a placeholder to temporarily store the header callback data.
121-
$header_callback_data = new \stdClass();
122-
$header_callback_data->rawResponseHeaders = '';
123-
$header_callback_data->responseCookies = array();
124-
$this->headerCallbackData = $header_callback_data;
125-
$this->setOpt(CURLOPT_HEADERFUNCTION, createHeaderCallback($header_callback_data));
126-
127-
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
128-
$this->headers = new CaseInsensitiveArray();
129-
$this->setUrl($base_url);
115+
$this->initialize($base_url);
130116
}
131117

132118
/**
@@ -1206,6 +1192,22 @@ public function verbose($on = true, $output = STDERR)
12061192
$this->setOpt(CURLOPT_STDERR, $output);
12071193
}
12081194

1195+
/**
1196+
* Reset
1197+
*
1198+
* @access public
1199+
*/
1200+
public function reset()
1201+
{
1202+
if (function_exists('curl_reset') && is_resource($this->curl)) {
1203+
curl_reset($this->curl);
1204+
} else {
1205+
$this->curl = curl_init();
1206+
}
1207+
1208+
$this->initialize();
1209+
}
1210+
12091211
/**
12101212
* Destruct
12111213
*
@@ -1485,6 +1487,32 @@ private function setEncodedCookie($key, $value)
14851487

14861488
$this->cookies[implode('', $name_chars)] = implode('', $value_chars);
14871489
}
1490+
1491+
/**
1492+
* Initialize
1493+
*
1494+
* @access private
1495+
* @param $base_url
1496+
* @throws \ErrorException
1497+
*/
1498+
private function initialize($base_url = null)
1499+
{
1500+
$this->id = uniqid('', true);
1501+
$this->setDefaultUserAgent();
1502+
$this->setDefaultTimeout();
1503+
$this->setOpt(CURLINFO_HEADER_OUT, true);
1504+
1505+
// Create a placeholder to temporarily store the header callback data.
1506+
$header_callback_data = new \stdClass();
1507+
$header_callback_data->rawResponseHeaders = '';
1508+
$header_callback_data->responseCookies = array();
1509+
$this->headerCallbackData = $header_callback_data;
1510+
$this->setOpt(CURLOPT_HEADERFUNCTION, createHeaderCallback($header_callback_data));
1511+
1512+
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
1513+
$this->headers = new CaseInsensitiveArray();
1514+
$this->setUrl($base_url);
1515+
}
14881516
}
14891517

14901518
/**

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3639,4 +3639,22 @@ public function testRelativeUrl()
36393639
);
36403640
}
36413641
}
3642+
3643+
public function testReset()
3644+
{
3645+
$php_version = preg_replace('/([\.\+\?\*\(\)\[\]\^\$\/])/', '\\\\\1', 'PHP/' . PHP_VERSION);
3646+
3647+
$test = new Test();
3648+
3649+
$user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT'));
3650+
$this->assertRegExp('/' . $php_version . '/', $user_agent);
3651+
3652+
$test->curl->setUserAgent('New agent');
3653+
$user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT'));
3654+
$this->assertEquals('New agent', $user_agent);
3655+
3656+
$test->curl->reset();
3657+
$user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT'));
3658+
$this->assertRegExp('/' . $php_version . '/', $user_agent);
3659+
}
36423660
}

0 commit comments

Comments
 (0)