5 Commits

Author SHA1 Message Date
Ahmad Nassri
4a480b156b Merge pull request #89 from RusinovIG/master
Fixed setting of default headers array
2016-01-20 14:41:35 -05:00
Igor Rusinov
ca6a3b1daf Fixed setting of default headers array 2016-01-18 23:24:14 +03:00
Ahmad Nassri
da71f063cf Merge pull request #88 from frankdee/master
improved solution to override cURL options …
2015-12-15 17:22:11 -05:00
frankdee
be5ee63ad1 shortened cURL options array merging 2015-12-14 19:17:31 +01:00
frankdee
32334a6bfa 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)
2015-12-14 13:55:49 +01:00
2 changed files with 44 additions and 8 deletions

View File

@@ -84,7 +84,7 @@ class Request
*/
public static function defaultHeaders($headers)
{
return array_merge(self::$defaultHeaders, $headers);
return self::$defaultHeaders = array_merge(self::$defaultHeaders, $headers);
}
/**
@@ -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,15 @@ 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)
{
$existing_options = $new_options + $existing_options;
return $existing_options;
}
}

View File

@@ -39,6 +39,31 @@ class UnirestRequestTest extends \PHPUnit_Framework_TestCase
Unirest\Request::timeout(null); // Cleaning timeout for the other tests
}
public function testDefaultHeaders()
{
$defaultHeaders = array(
'header1' => 'Hello',
'header2' => 'world'
);
Unirest\Request::defaultHeaders($defaultHeaders);
$response = Unirest\Request::get('http://mockbin.com/request');
$this->assertEquals(200, $response->code);
$this->assertObjectHasAttribute('header1', $response->body->headers);
$this->assertEquals('Hello', $response->body->headers->header1);
$this->assertObjectHasAttribute('header2', $response->body->headers);
$this->assertEquals('world', $response->body->headers->header2);
Unirest\Request::clearDefaultHeaders();
$response = Unirest\Request::get('http://mockbin.com/request');
$this->assertEquals(200, $response->code);
$this->assertObjectNotHasAttribute('header1', $response->body->headers);
$this->assertObjectNotHasAttribute('header2', $response->body->headers);
}
public function testDefaultHeader()
{
Unirest\Request::defaultHeader('Hello', 'custom');