Skip to content

Commit 98c9d8f

Browse files
committed
Merge pull request php-curl-class#300 from zachborboa/299
Fix php-curl-class#299: Add Curl::getResponseCookies()
2 parents 14bc316 + c9f785e commit 98c9d8f

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Curl::get($url, $data = array())
187187
Curl::getCookie($key)
188188
Curl::getOpt($option)
189189
Curl::getResponseCookie($key)
190+
Curl::getResponseCookies()
190191
Curl::head($url, $data = array())
191192
Curl::headerCallback($ch, $header)
192193
Curl::options($url, $data = array())

src/Curl/Curl.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ public function head($url, $data = array())
445445
*/
446446
public function headerCallback($ch, $header)
447447
{
448-
if (preg_match('/^Set-Cookie:\s*([^=]+)=([^;]+)/mi', $header, $cookie) == 1) {
449-
$this->responseCookies[$cookie[1]] = $cookie[2];
448+
if (preg_match('/^Set-Cookie:\s*([^=]+)=([^;]+)/mi', $header, $cookie) === 1) {
449+
$this->responseCookies[$cookie[1]] = trim($cookie[2], " \n\r\t\0\x0B");
450450
}
451451
$this->rawResponseHeaders .= $header;
452452
return strlen($header);
@@ -632,6 +632,17 @@ public function getResponseCookie($key)
632632
return isset($this->responseCookies[$key]) ? $this->responseCookies[$key] : null;
633633
}
634634

635+
/**
636+
* Get response cookies.
637+
*
638+
* @access public
639+
* @return array
640+
*/
641+
public function getResponseCookies()
642+
{
643+
return $this->responseCookies;
644+
}
645+
635646
/**
636647
* Set Port
637648
*

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,10 @@ public function testMultipleCookieResponse()
753753
$test = new Test();
754754
$test->server('multiple_cookie', 'GET');
755755
$this->assertEquals('cookie1=scrumptious,cookie2=mouthwatering', $test->curl->responseHeaders['Set-Cookie']);
756+
757+
$response_cookies = $test->curl->getResponseCookies();
758+
$this->assertEquals('scrumptious', $response_cookies['cookie1']);
759+
$this->assertEquals('mouthwatering', $response_cookies['cookie2']);
756760
}
757761

758762
public function testDefaultTimeout()

0 commit comments

Comments
 (0)