Implement Curl::setOpts()

This commit is contained in:
Zach Borboa
2016-08-07 22:41:12 -07:00
parent b1f345b2f3
commit 26701569b2
3 changed files with 36 additions and 1 deletions
+1
View File
@@ -216,6 +216,7 @@ Curl::setDigestAuthentication($username, $password = '')
Curl::setHeader($key, $value)
Curl::setJsonDecoder($function)
Curl::setOpt($option, $value)
Curl::setOpts($options)
Curl::setPort($port)
Curl::setReferer($referer)
Curl::setReferrer($referrer)
+19
View File
@@ -899,6 +899,25 @@ class Curl
return $success;
}
/**
* Set Opts
*
* @access public
* @param $options
*
* @return boolean
* Returns TRUE if all options were successfully set. If an option could not be successfully set, FALSE is
* immediately returned, ignoring any future options in the options array. Similar to curl_setopt_array().
*/
public function setOpts($options)
{
foreach ($options as $option => $value) {
if (!$this->setOpt($option, $value)) {
return false;
}
}
}
/**
* Set Referer
*
+16 -1
View File
@@ -2759,6 +2759,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
{
$option = CURLOPT_ENCODING;
$value = 'gzip';
$null = chr(0);
// Ensure the option is stored when curl_setopt() succeeds.
$curl = new Curl();
@@ -2775,7 +2776,6 @@ class CurlTest extends PHPUnit_Framework_TestCase
// Ensure the option is not stored when curl_setopt() fails. Make curl_setopt() return false and suppress
// errors. Triggers warning: "curl_setopt(): Curl option contains invalid characters (\0)".
$null = chr(0);
$curl = new Curl();
$success = @$curl->setOpt($option, $null);
@@ -2786,5 +2786,20 @@ class CurlTest extends PHPUnit_Framework_TestCase
$this->assertFalse($success);
$this->assertFalse(isset($options[$option]));
// Ensure options following a Curl::setOpt() failure are not set when using Curl::setOpts().
$options = array(
$option => $null,
CURLOPT_COOKIE => 'a=b',
);
$curl = new Curl();
@$curl->setOpts($options);
$reflector = new ReflectionObject($curl);
$property = $reflector->getProperty('options');
$property->setAccessible(true);
$options = $property->getValue($curl);
$this->assertFalse(isset($options[CURLOPT_COOKIE]));
}
}