Skip to content

Commit

Permalink
improved solution to override cURL options, that preserves the right …
Browse files Browse the repository at this point in the history
…indices in cURL option arrays

(Note: PHP seems to not override all options when calling 'curl_setopt_array(..)' several times after another)
  • Loading branch information
frankdee committed Dec 14, 2015
1 parent e11c54d commit 32334a6
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/Unirest/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ public static function clearDefaultHeaders()
* Set curl options to send on every request
*
* @param array $options options array
* @return array
*/
public static function curlOpts($opts)
public static function curlOpts($options)
{
return array_merge(self::$curlOpts, $opts);
return self::mergeCurlOptions(self::$curlOpts, $options);
}

/**
Expand Down Expand Up @@ -405,7 +406,7 @@ public static function send($method, $url, $body = null, $headers = array(), $us
$url .= urldecode(http_build_query(self::buildHTTPCurlQuery($body)));
}

curl_setopt_array(self::$handle, array(
$curl_base_options = [
CURLOPT_URL => self::encodeUrl($url),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
Expand All @@ -417,10 +418,9 @@ public static function send($method, $url, $body = null, $headers = array(), $us
CURLOPT_SSL_VERIFYHOST => self::$verifyHost === false ? 0 : 2,
// If an empty string, '', is set, a header containing all supported encoding types is sent
CURLOPT_ENCODING => ''
));

// update options
curl_setopt_array(self::$handle, self::$curlOpts);
];

curl_setopt_array(self::$handle, self::mergeCurlOptions($curl_base_options, self::$curlOpts));

if (self::$socketTimeout !== null) {
curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout);
Expand Down Expand Up @@ -558,4 +558,18 @@ private static function getHeaderString($key, $val)
$key = trim(strtolower($key));
return $key . ': ' . $val;
}

/**
* @param array $existing_options
* @param array $new_options
* @return array
*/
private static function mergeCurlOptions(&$existing_options, $new_options)
{
foreach($new_options as $key => $value){
$existing_options[$key] = $value;
}

return $existing_options;
}
}

0 comments on commit 32334a6

Please sign in to comment.