From 1fbf55d15f8134f7c59e89f92dab44dfcf391a4d Mon Sep 17 00:00:00 2001 From: Chengke Sun Date: Wed, 11 May 2016 14:12:10 +0800 Subject: [PATCH 1/5] Add setCookieString method on Curl.php Add setCookieString method to parse the string cookies and call setCookie method. --- src/Curl/Curl.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index f4f8911..b2dd8a2 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -697,7 +697,26 @@ class Curl { $this->setOpt(CURLOPT_CONNECTTIMEOUT, $seconds); } - + + /** + * Set Cookie String + * + * @access public + * @param $string + */ + public function setCookieString($string) + { + if(preg_match_all('/\s*([^;=]+)=([^;]+)/i',$string,$matches) > 0){ + if(isset($matches[1]) && isset($matches[2])){ + if(count($matches[1]) == count($matches[2])){ + foreach ($matches[1] as $handle => $key) { + $this->setCookie($key, $matches[2][$handle]); + } + } + } + } + } + /** * Set Cookie File * From 46a7694b7924608c79329723115801ac11e500ae Mon Sep 17 00:00:00 2001 From: Chengke Sun Date: Wed, 11 May 2016 20:41:46 +0800 Subject: [PATCH 2/5] Update Curl.php More rigorous comparison --- 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 b2dd8a2..313deed 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -708,7 +708,7 @@ class Curl { if(preg_match_all('/\s*([^;=]+)=([^;]+)/i',$string,$matches) > 0){ if(isset($matches[1]) && isset($matches[2])){ - if(count($matches[1]) == count($matches[2])){ + if(count($matches[1]) === count($matches[2])){ foreach ($matches[1] as $handle => $key) { $this->setCookie($key, $matches[2][$handle]); } From 2e4a868a395bb9a282576d272ad4734e2239f060 Mon Sep 17 00:00:00 2001 From: Chengke Sun Date: Wed, 11 May 2016 21:20:31 +0800 Subject: [PATCH 3/5] Update Curl.php --- src/Curl/Curl.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 313deed..c965ae2 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -697,7 +697,7 @@ class Curl { $this->setOpt(CURLOPT_CONNECTTIMEOUT, $seconds); } - + /** * Set Cookie String * @@ -716,7 +716,7 @@ class Curl } } } - + /** * Set Cookie File * From 6085204f2fa7c83a4b996c159b3dddec124f959d Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 17 Jul 2016 14:41:50 -0700 Subject: [PATCH 4/5] Simplify Curl::setCookieString --- README.md | 1 + src/Curl/Curl.php | 10 +--------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index dd89e3c..85b8399 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ Curl::setConnectTimeout($seconds) Curl::setCookie($key, $value) Curl::setCookieFile($cookie_file) Curl::setCookieJar($cookie_jar) +Curl::setCookieString($string) Curl::setDefaultJsonDecoder() Curl::setDefaultTimeout() Curl::setDefaultUserAgent() diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 95dab62..aae4472 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -721,15 +721,7 @@ class Curl */ public function setCookieString($string) { - if(preg_match_all('/\s*([^;=]+)=([^;]+)/i',$string,$matches) > 0){ - if(isset($matches[1]) && isset($matches[2])){ - if(count($matches[1]) === count($matches[2])){ - foreach ($matches[1] as $handle => $key) { - $this->setCookie($key, $matches[2][$handle]); - } - } - } - } + return $this->setOpt(CURLOPT_COOKIE, $string); } /** From 15efc64824ecb90fba26b024b5caa186253306f1 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 17 Jul 2016 14:42:55 -0700 Subject: [PATCH 5/5] Add test for Curl::setCookieString --- tests/PHPCurlClass/PHPCurlClassTest.php | 23 +++++++++++++++++++---- tests/PHPCurlClass/server.php | 8 +++++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 0e38938..3844d45 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -706,7 +706,7 @@ class CurlTest extends PHPUnit_Framework_TestCase } } - public function testCookies() + public function testSetCookie() { $test = new Test(); $test->curl->setCookie('mycookie', 'yum'); @@ -715,7 +715,7 @@ class CurlTest extends PHPUnit_Framework_TestCase ))); } - public function testCookieEncodingSpace() + public function testSetCookieEncodingSpace() { $curl = new Curl(); $curl->setCookie('cookie', 'Om nom nom nom'); @@ -727,7 +727,7 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals('cookie=Om%20nom%20nom%20nom', $options[CURLOPT_COOKIE]); } - public function testMultipleCookies() + public function testSetMultipleCookies() { $curl = new Curl(); $curl->setCookie('cookie', 'Om nom nom nom'); @@ -740,7 +740,7 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals('cookie=Om%20nom%20nom%20nom; foo=bar', $options[CURLOPT_COOKIE]); } - public function testCookieEncodingColon() + public function testSetCookieEncodingColon() { $curl = new Curl(); $curl->setCookie('JSESSIONID', '0000wd-PcsB3bZ-KzYGAqm_rKlm:17925chrl'); @@ -752,6 +752,21 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals('JSESSIONID=0000wd-PcsB3bZ-KzYGAqm_rKlm:17925chrl', $options[CURLOPT_COOKIE]); } + public function testSetCookieString() + { + $cookie_string = 'fruit=apple; color=red'; + + $test = new Test(); + $test->curl->setCookieString($cookie_string); + + $reflectionClass = new ReflectionClass('\Curl\Curl'); + $reflectionProperty = $reflectionClass->getProperty('options'); + $reflectionProperty->setAccessible(true); + $options = $reflectionProperty->getValue($test->curl); + $this->assertEquals($cookie_string, $options[CURLOPT_COOKIE]); + $this->assertEquals('fruit=apple&color=red', $test->server('cookie', 'GET')); + } + public function testCookieFile() { $cookie_file = dirname(__FILE__) . '/cookies.txt'; diff --git a/tests/PHPCurlClass/server.php b/tests/PHPCurlClass/server.php index da7ea02..124f165 100644 --- a/tests/PHPCurlClass/server.php +++ b/tests/PHPCurlClass/server.php @@ -299,6 +299,12 @@ $data_mapping = array( if (!empty($test)) { $data = $data_mapping[$test]; - $value = isset($data[$key]) ? $data[$key] : ''; + if (empty($key)) { + // Return all values when a key is not specified. + $value = http_build_query($data); + } else { + // Return individual value when a key is specified. + $value = isset($data[$key]) ? $data[$key] : ''; + } echo $value; }