From e08e9b43eadeb8d84761bfb1a415605409d03b13 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 7 Aug 2016 21:01:10 -0700 Subject: [PATCH 1/7] Update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5ad1832..ff6ed55 100644 --- a/README.md +++ b/README.md @@ -225,8 +225,8 @@ Curl::setUserAgent($user_agent) Curl::setXmlDecoder($function) Curl::success($callback) Curl::unsetHeader($key) -Curl::verbose($on = true, $output=STDERR) -Curl::http_build_multi_query($data, $key = null) +Curl::verbose($on = true, $output = STDERR) +Curl::array_flatten_multidim($array, $prefix = false) { Curl::is_array_assoc($array) Curl::is_array_multidim($array) MultiCurl::__construct($base_url = null) From a8c2b53fa1b26b0f2bf05aef5e3cb44c0a117c00 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 7 Aug 2016 21:11:25 -0700 Subject: [PATCH 2/7] Store option set only when option successfully set --- src/Curl/Curl.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 6ab15d3..7f07215 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -892,8 +892,11 @@ class Curl trigger_error($required_options[$option] . ' is a required option', E_USER_WARNING); } - $this->options[$option] = $value; - return curl_setopt($this->curl, $option, $value); + $success = curl_setopt($this->curl, $option, $value); + if ($success) { + $this->options[$option] = $value; + } + return $success; } /** From b1f345b2f3ebe55f5c34b0c80e0804af3f5b330c Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 7 Aug 2016 22:24:39 -0700 Subject: [PATCH 3/7] Add test for Curl::setOpt --- tests/PHPCurlClass/PHPCurlClassTest.php | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 3d7bc84..39967ee 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -2754,4 +2754,37 @@ class CurlTest extends PHPUnit_Framework_TestCase $test->server('request_method', 'GET'); $this->assertTrue(is_float($test->curl->totalTime)); } + + public function testOptionSet() + { + $option = CURLOPT_ENCODING; + $value = 'gzip'; + + // Ensure the option is stored when curl_setopt() succeeds. + $curl = new Curl(); + $success = $curl->setOpt($option, $value); + + $reflector = new ReflectionObject($curl); + $property = $reflector->getProperty('options'); + $property->setAccessible(true); + $options = $property->getValue($curl); + + $this->assertTrue($success); + $this->assertTrue(isset($options[$option])); + $this->assertEquals($value, $options[$option]); + + // 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); + + $reflector = new ReflectionObject($curl); + $property = $reflector->getProperty('options'); + $property->setAccessible(true); + $options = $property->getValue($curl); + + $this->assertFalse($success); + $this->assertFalse(isset($options[$option])); + } } From 26701569b2641c3a2f0b611baf765ebcac9d79b7 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 7 Aug 2016 22:41:12 -0700 Subject: [PATCH 4/7] Implement Curl::setOpts() --- README.md | 1 + src/Curl/Curl.php | 19 +++++++++++++++++++ tests/PHPCurlClass/PHPCurlClassTest.php | 17 ++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ff6ed55..b802ebb 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 7f07215..d47bf29 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -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 * diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 39967ee..26969b9 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -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])); } } From 9aca3a996c8eb60d0744829e182672b78ea32b34 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 7 Aug 2016 22:42:38 -0700 Subject: [PATCH 5/7] Clean up --- README.md | 2 +- src/Curl/Curl.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b802ebb..1644237 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,7 @@ Curl::setXmlDecoder($function) Curl::success($callback) Curl::unsetHeader($key) Curl::verbose($on = true, $output = STDERR) -Curl::array_flatten_multidim($array, $prefix = false) { +Curl::array_flatten_multidim($array, $prefix = false) Curl::is_array_assoc($array) Curl::is_array_multidim($array) MultiCurl::__construct($base_url = null) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index d47bf29..a362722 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -1220,7 +1220,8 @@ class Curl * * @return array */ - public static function array_flatten_multidim($array, $prefix = false) { + public static function array_flatten_multidim($array, $prefix = false) + { $return = array(); if (is_array($array) || is_object($array)) { if (empty($array)) { From 1ad73d5da0200340b4d688eb571d63001104fa49 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 7 Aug 2016 22:55:35 -0700 Subject: [PATCH 6/7] Remove all caps --- src/Curl/Curl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index a362722..e311ed8 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -906,7 +906,7 @@ class Curl * @param $options * * @return boolean - * Returns TRUE if all options were successfully set. If an option could not be successfully set, FALSE is + * 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) From e705f5e86e0f5a7871ef0884fb61fee03360621e Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 7 Aug 2016 23:18:36 -0700 Subject: [PATCH 7/7] Skip setOpt test on 5.3, 5.4, and HHVM --- tests/PHPCurlClass/PHPCurlClassTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 26969b9..309518c 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -2757,6 +2757,11 @@ class CurlTest extends PHPUnit_Framework_TestCase public function testOptionSet() { + // Skip this test on 5.3, 5.4, and HHVM. + if (version_compare(PHP_VERSION, '5.5.0', '<') || defined('HHVM_VERSION')) { + return; + } + $option = CURLOPT_ENCODING; $value = 'gzip'; $null = chr(0);