Merge pull request #526 from zachborboa/master

Implement url::reset()
This commit is contained in:
Zach Borboa
2018-06-04 23:35:28 -07:00
committed by GitHub
4 changed files with 60 additions and 15 deletions
+1
View File
@@ -219,6 +219,7 @@ Curl::post($url, $data = '', $follow_303_with_post = false)
Curl::progress($callback)
Curl::put($url, $data = array())
Curl::removeHeader($key)
Curl::reset()
Curl::search($url, $data = array())
Curl::setBasicAuthentication($username, $password = '')
Curl::setConnectTimeout($seconds)
+1
View File
@@ -18,6 +18,7 @@
"ext-mbstring": "*"
},
"require-dev": {
"ext-gd": "*",
"phpunit/phpunit": "*",
"squizlabs/php_codesniffer": "*"
},
+42 -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,31 @@ class Curl
$this->cookies[implode('', $name_chars)] = implode('', $value_chars);
}
/**
* Initialize
*
* @access private
* @param $base_url
*/
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);
}
}
/**
+16
View File
@@ -3639,4 +3639,20 @@ class CurlTest extends \PHPUnit\Framework\TestCase
);
}
}
public function testReset()
{
$test = new Test();
$original_user_agent = $test->server('server', 'GET', array('key' => 'HTTP_USER_AGENT'));
$this->assertNotEquals('New agent', $original_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->assertEquals($original_user_agent, $user_agent);
}
}