mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-31 04:34:27 +00:00
Merge pull request #788 from zachborboa/cleanup
Differentiate between internal options and user-set options
This commit is contained in:
@@ -10,6 +10,7 @@ abstract class BaseCurl
|
||||
public $completeCallback = null;
|
||||
|
||||
protected $options = [];
|
||||
protected $userSetOptions = [];
|
||||
|
||||
/**
|
||||
* Before Send
|
||||
@@ -157,6 +158,11 @@ abstract class BaseCurl
|
||||
$this->setOpt(CURLOPT_FILE, $file);
|
||||
}
|
||||
|
||||
protected function setFileInternal($file)
|
||||
{
|
||||
$this->setOptInternal(CURLOPT_FILE, $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set follow location
|
||||
*
|
||||
@@ -209,6 +215,11 @@ abstract class BaseCurl
|
||||
}
|
||||
|
||||
abstract public function setOpt($option, $value);
|
||||
|
||||
protected function setOptInternal($option, $value)
|
||||
{
|
||||
}
|
||||
|
||||
abstract public function setOpts($options);
|
||||
|
||||
/**
|
||||
@@ -294,6 +305,11 @@ abstract class BaseCurl
|
||||
$this->setOpt(CURLOPT_RANGE, $range);
|
||||
}
|
||||
|
||||
protected function setRangeInternal($range)
|
||||
{
|
||||
$this->setOptInternal(CURLOPT_RANGE, $range);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Referer
|
||||
*
|
||||
@@ -329,6 +345,11 @@ abstract class BaseCurl
|
||||
$this->setOpt(CURLOPT_TIMEOUT, $seconds);
|
||||
}
|
||||
|
||||
protected function setTimeoutInternal($seconds)
|
||||
{
|
||||
$this->setOptInternal(CURLOPT_TIMEOUT, $seconds);
|
||||
}
|
||||
|
||||
abstract public function setUrl($url, $mixed_data = '');
|
||||
|
||||
/**
|
||||
@@ -342,6 +363,11 @@ abstract class BaseCurl
|
||||
$this->setOpt(CURLOPT_USERAGENT, $user_agent);
|
||||
}
|
||||
|
||||
protected function setUserAgentInternal($user_agent)
|
||||
{
|
||||
$this->setOptInternal(CURLOPT_USERAGENT, $user_agent);
|
||||
}
|
||||
|
||||
abstract public function setXmlDecoder($mixed);
|
||||
abstract public function stop();
|
||||
|
||||
|
||||
+80
-15
@@ -247,6 +247,7 @@ class Curl extends BaseCurl
|
||||
}
|
||||
$this->curl = null;
|
||||
$this->options = null;
|
||||
$this->userSetOptions = null;
|
||||
$this->jsonDecoder = null;
|
||||
$this->jsonDecoderArgs = null;
|
||||
$this->xmlDecoder = null;
|
||||
@@ -267,6 +268,12 @@ class Curl extends BaseCurl
|
||||
$this->setOpt(CURLOPT_NOPROGRESS, false);
|
||||
}
|
||||
|
||||
private function progressInternal($callback)
|
||||
{
|
||||
$this->setOptInternal(CURLOPT_PROGRESSFUNCTION, $callback);
|
||||
$this->setOptInternal(CURLOPT_NOPROGRESS, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
@@ -552,7 +559,7 @@ class Curl extends BaseCurl
|
||||
$this->unsetHeader('Content-Length');
|
||||
|
||||
// Reset nobody setting possibly set from a HEAD request.
|
||||
$this->setOpt(CURLOPT_NOBODY, false);
|
||||
$this->setOptInternal(CURLOPT_NOBODY, false);
|
||||
|
||||
// Allow multicurl to attempt retry as needed.
|
||||
if ($this->isChildOfMultiCurl()) {
|
||||
@@ -600,8 +607,8 @@ class Curl extends BaseCurl
|
||||
$url = (string)$this->url;
|
||||
}
|
||||
$this->setUrl($url, $data);
|
||||
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
|
||||
$this->setOpt(CURLOPT_HTTPGET, true);
|
||||
$this->setOptInternal(CURLOPT_CUSTOMREQUEST, 'GET');
|
||||
$this->setOptInternal(CURLOPT_HTTPGET, true);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
@@ -968,6 +975,11 @@ class Curl extends BaseCurl
|
||||
$this->setOpt(CURLINFO_HEADER_OUT, true);
|
||||
}
|
||||
|
||||
private function setDefaultHeaderOutInternal()
|
||||
{
|
||||
$this->setOptInternal(CURLINFO_HEADER_OUT, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Default Timeout
|
||||
*
|
||||
@@ -978,18 +990,33 @@ class Curl extends BaseCurl
|
||||
$this->setTimeout(self::DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
private function setDefaultTimeoutInternal()
|
||||
{
|
||||
$this->setTimeoutInternal(self::DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Default User Agent
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setDefaultUserAgent()
|
||||
{
|
||||
$this->setUserAgent($this->getDefaultUserAgent());
|
||||
}
|
||||
|
||||
private function setDefaultUserAgentInternal()
|
||||
{
|
||||
$this->setUserAgentInternal($this->getDefaultUserAgent());
|
||||
}
|
||||
|
||||
private function getDefaultUserAgent()
|
||||
{
|
||||
$user_agent = 'PHP-Curl-Class/' . self::VERSION . ' (+https://github.com/php-curl-class/php-curl-class)';
|
||||
$user_agent .= ' PHP/' . PHP_VERSION;
|
||||
$curl_version = curl_version();
|
||||
$user_agent .= ' curl/' . $curl_version['version'];
|
||||
$this->setUserAgent($user_agent);
|
||||
return $user_agent;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1091,6 +1118,25 @@ class Curl extends BaseCurl
|
||||
trigger_error($required_options[$option] . ' is a required option', E_USER_WARNING);
|
||||
}
|
||||
|
||||
$success = curl_setopt($this->curl, $option, $value);
|
||||
if ($success) {
|
||||
$this->options[$option] = $value;
|
||||
$this->userSetOptions[$option] = $value;
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Opt Internal
|
||||
*
|
||||
* @access protected
|
||||
* @param $option
|
||||
* @param $value
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function setOptInternal($option, $value)
|
||||
{
|
||||
$success = curl_setopt($this->curl, $option, $value);
|
||||
if ($success) {
|
||||
$this->options[$option] = $value;
|
||||
@@ -1132,6 +1178,11 @@ class Curl extends BaseCurl
|
||||
$this->setOpt(CURLOPT_PROTOCOLS, $protocols);
|
||||
}
|
||||
|
||||
private function setProtocolsInternal($protocols)
|
||||
{
|
||||
$this->setOptInternal(CURLOPT_PROTOCOLS, $protocols);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Retry
|
||||
*
|
||||
@@ -1170,6 +1221,11 @@ class Curl extends BaseCurl
|
||||
$this->setOpt(CURLOPT_REDIR_PROTOCOLS, $redirect_protocols);
|
||||
}
|
||||
|
||||
private function setRedirectProtocolsInternal($redirect_protocols)
|
||||
{
|
||||
$this->setOptInternal(CURLOPT_REDIR_PROTOCOLS, $redirect_protocols);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Url
|
||||
*
|
||||
@@ -1431,9 +1487,9 @@ class Curl extends BaseCurl
|
||||
$this->curl = curl_init();
|
||||
}
|
||||
|
||||
$this->setDefaultUserAgent();
|
||||
$this->setDefaultTimeout();
|
||||
$this->setDefaultHeaderOut();
|
||||
$this->setDefaultUserAgentInternal();
|
||||
$this->setDefaultTimeoutInternal();
|
||||
$this->setDefaultHeaderOutInternal();
|
||||
|
||||
$this->initialize();
|
||||
}
|
||||
@@ -2005,8 +2061,8 @@ class Curl extends BaseCurl
|
||||
*/
|
||||
private function initialize($base_url = null, $options = [])
|
||||
{
|
||||
$this->setProtocols(CURLPROTO_HTTPS | CURLPROTO_HTTP);
|
||||
$this->setRedirectProtocols(CURLPROTO_HTTPS | CURLPROTO_HTTP);
|
||||
$this->setProtocolsInternal(CURLPROTO_HTTPS | CURLPROTO_HTTP);
|
||||
$this->setRedirectProtocolsInternal(CURLPROTO_HTTPS | CURLPROTO_HTTP);
|
||||
|
||||
if (isset($options)) {
|
||||
$this->setOpts($options);
|
||||
@@ -2016,16 +2072,16 @@ class Curl extends BaseCurl
|
||||
|
||||
// Only set default user agent if not already set.
|
||||
if (!array_key_exists(CURLOPT_USERAGENT, $this->options)) {
|
||||
$this->setDefaultUserAgent();
|
||||
$this->setDefaultUserAgentInternal();
|
||||
}
|
||||
|
||||
// Only set default timeout if not already set.
|
||||
if (!array_key_exists(CURLOPT_TIMEOUT, $this->options)) {
|
||||
$this->setDefaultTimeout();
|
||||
$this->setDefaultTimeoutInternal();
|
||||
}
|
||||
|
||||
if (!array_key_exists(CURLINFO_HEADER_OUT, $this->options)) {
|
||||
$this->setDefaultHeaderOut();
|
||||
$this->setDefaultHeaderOutInternal();
|
||||
}
|
||||
|
||||
// Create a placeholder to temporarily store the header callback data.
|
||||
@@ -2035,10 +2091,10 @@ class Curl extends BaseCurl
|
||||
$header_callback_data->stopRequestDecider = null;
|
||||
$header_callback_data->stopRequest = false;
|
||||
$this->headerCallbackData = $header_callback_data;
|
||||
$this->setStop();
|
||||
$this->setOpt(CURLOPT_HEADERFUNCTION, createHeaderCallback($header_callback_data));
|
||||
$this->setStopInternal();
|
||||
$this->setOptInternal(CURLOPT_HEADERFUNCTION, createHeaderCallback($header_callback_data));
|
||||
|
||||
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
|
||||
$this->setOptInternal(CURLOPT_RETURNTRANSFER, true);
|
||||
$this->headers = new CaseInsensitiveArray();
|
||||
|
||||
if ($base_url !== null) {
|
||||
@@ -2075,6 +2131,15 @@ class Curl extends BaseCurl
|
||||
$this->progress(createStopRequestFunction($header_callback_data));
|
||||
}
|
||||
|
||||
private function setStopInternal($callback = null)
|
||||
{
|
||||
$this->headerCallbackData->stopRequestDecider = $callback;
|
||||
$this->headerCallbackData->stopRequest = false;
|
||||
|
||||
$header_callback_data = $this->headerCallbackData;
|
||||
$this->progressInternal(createStopRequestFunction($header_callback_data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user