Skip to content

Commit

Permalink
Merge pull request #88 from frankdee/master
Browse files Browse the repository at this point in the history
improved solution to override cURL options …
  • Loading branch information
Ahmad Nassri committed Dec 15, 2015
2 parents e11c54d + be5ee63 commit da71f06
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 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,15 @@ 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)
{
$existing_options = $new_options + $existing_options;
return $existing_options;
}
}

0 comments on commit da71f06

Please sign in to comment.