From 32334a6bfa81672ad2b86fe6de3384dd4eaa90fa Mon Sep 17 00:00:00 2001 From: frankdee Date: Mon, 14 Dec 2015 13:55:49 +0100 Subject: [PATCH 1/2] improved solution to override cURL options, that preserves the right indices in cURL option arrays (Note: PHP seems to not override all options when calling 'curl_setopt_array(..)' several times after another) --- src/Unirest/Request.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Unirest/Request.php b/src/Unirest/Request.php index 44e3df9..5f04406 100755 --- a/src/Unirest/Request.php +++ b/src/Unirest/Request.php @@ -110,10 +110,11 @@ class Request * 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); } /** @@ -405,7 +406,7 @@ class Request $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, @@ -417,10 +418,9 @@ class Request 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); @@ -558,4 +558,18 @@ class Request $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; + } } From be5ee63ad1e1247100b402a323d1a73d9a98c8af Mon Sep 17 00:00:00 2001 From: frankdee Date: Mon, 14 Dec 2015 19:17:31 +0100 Subject: [PATCH 2/2] shortened cURL options array merging --- src/Unirest/Request.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Unirest/Request.php b/src/Unirest/Request.php index 5f04406..50b744d 100755 --- a/src/Unirest/Request.php +++ b/src/Unirest/Request.php @@ -566,10 +566,7 @@ class Request */ private static function mergeCurlOptions(&$existing_options, $new_options) { - foreach($new_options as $key => $value){ - $existing_options[$key] = $value; - } - + $existing_options = $new_options + $existing_options; return $existing_options; } }