Skip to content

Commit da71f06

Browse files
author
Ahmad Nassri
committed
Merge pull request #88 from frankdee/master
improved solution to override cURL options …
2 parents e11c54d + be5ee63 commit da71f06

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/Unirest/Request.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ public static function clearDefaultHeaders()
110110
* Set curl options to send on every request
111111
*
112112
* @param array $options options array
113+
* @return array
113114
*/
114-
public static function curlOpts($opts)
115+
public static function curlOpts($options)
115116
{
116-
return array_merge(self::$curlOpts, $opts);
117+
return self::mergeCurlOptions(self::$curlOpts, $options);
117118
}
118119

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

408-
curl_setopt_array(self::$handle, array(
409+
$curl_base_options = [
409410
CURLOPT_URL => self::encodeUrl($url),
410411
CURLOPT_RETURNTRANSFER => true,
411412
CURLOPT_FOLLOWLOCATION => true,
@@ -417,10 +418,9 @@ public static function send($method, $url, $body = null, $headers = array(), $us
417418
CURLOPT_SSL_VERIFYHOST => self::$verifyHost === false ? 0 : 2,
418419
// If an empty string, '', is set, a header containing all supported encoding types is sent
419420
CURLOPT_ENCODING => ''
420-
));
421-
422-
// update options
423-
curl_setopt_array(self::$handle, self::$curlOpts);
421+
];
422+
423+
curl_setopt_array(self::$handle, self::mergeCurlOptions($curl_base_options, self::$curlOpts));
424424

425425
if (self::$socketTimeout !== null) {
426426
curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout);
@@ -558,4 +558,15 @@ private static function getHeaderString($key, $val)
558558
$key = trim(strtolower($key));
559559
return $key . ': ' . $val;
560560
}
561+
562+
/**
563+
* @param array $existing_options
564+
* @param array $new_options
565+
* @return array
566+
*/
567+
private static function mergeCurlOptions(&$existing_options, $new_options)
568+
{
569+
$existing_options = $new_options + $existing_options;
570+
return $existing_options;
571+
}
561572
}

0 commit comments

Comments
 (0)