Skip to content

Commit b3aa206

Browse files
committed
Merge pull request php-curl-class#236 from fealXX/master
Added the ability to get cookie values (after a request).
2 parents 8094e2f + 740fa5b commit b3aa206

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/Curl/Curl.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Curl
3737
private $completeFunction = null;
3838

3939
private $cookies = array();
40+
private $responseCookies = array();
4041
private $headers = array();
4142
private $options = array();
4243

@@ -297,14 +298,14 @@ public function error($callback)
297298
*/
298299
public function exec($ch = null)
299300
{
301+
$this->responseCookies = array();
300302
if (!($ch === null)) {
301303
$this->rawResponse = curl_multi_getcontent($ch);
302304
} else {
303305
$this->call($this->beforeSendFunction);
304306
$this->rawResponse = curl_exec($this->curl);
305307
$this->curlErrorCode = curl_errno($this->curl);
306308
}
307-
308309
$this->curlErrorMessage = curl_error($this->curl);
309310
$this->curlError = !($this->curlErrorCode === 0);
310311
$this->httpStatusCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
@@ -401,6 +402,9 @@ public function head($url, $data = array())
401402
*/
402403
public function headerCallback($ch, $header)
403404
{
405+
if (preg_match('/^Set-Cookie:\s*([^=]+)=([^;]+)/mi', $header, $cookie) == 1) {
406+
$this->responseCookies[$cookie[1]] = $cookie[2];
407+
}
404408
$this->rawResponseHeaders .= $header;
405409
return strlen($header);
406410
}
@@ -539,6 +543,16 @@ public function setCookie($key, $value)
539543
$this->setOpt(CURLOPT_COOKIE, str_replace(' ', '%20', urldecode(http_build_query($this->cookies, '', '; '))));
540544
}
541545

546+
/**
547+
* get Cookie of Response, if Key is set. Otherwise return NULL
548+
*
549+
* @access public
550+
* @param $key
551+
*/
552+
public function getResponseCookie($key) {
553+
return (isset($this->responseCookies[$key]))?$this->responseCookies[$key]:NULL;
554+
}
555+
542556
/**
543557
* Set Port
544558
*

0 commit comments

Comments
 (0)