mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 12:14:36 +00:00
Improve cookie handling for multicurl requests.
Fix Curl::responseCookies set in headerCallback() being overwritten during multicurl request. Use rfc cookie encoding when using MultiCurl::setCookie(). Make MultiCurl::responseCookies publicly accessible.
This commit is contained in:
@@ -259,6 +259,7 @@ MultiCurl::setDigestAuthentication($username, $password = '')
|
||||
MultiCurl::setHeader($key, $value)
|
||||
MultiCurl::setJsonDecoder($function)
|
||||
MultiCurl::setOpt($option, $value)
|
||||
MultiCurl::setOpts($options)
|
||||
MultiCurl::setReferer($referer)
|
||||
MultiCurl::setReferrer($referrer)
|
||||
MultiCurl::setTimeout($seconds)
|
||||
|
||||
+2
-2
@@ -60,6 +60,7 @@ class Curl
|
||||
public $requestHeaders = null;
|
||||
public $responseHeaders = null;
|
||||
public $rawResponseHeaders = '';
|
||||
public $responseCookies = array();
|
||||
public $response = null;
|
||||
public $rawResponse = null;
|
||||
|
||||
@@ -71,7 +72,6 @@ class Curl
|
||||
public $fileHandle = null;
|
||||
|
||||
private $cookies = array();
|
||||
private $responseCookies = array();
|
||||
private $headers = array();
|
||||
private $options = array();
|
||||
|
||||
@@ -336,8 +336,8 @@ class Curl
|
||||
*/
|
||||
public function exec($ch = null)
|
||||
{
|
||||
$this->responseCookies = array();
|
||||
if ($ch === null) {
|
||||
$this->responseCookies = array();
|
||||
$this->call($this->beforeSendFunction);
|
||||
$this->rawResponse = curl_exec($this->curl);
|
||||
$this->curlErrorCode = curl_errno($this->curl);
|
||||
|
||||
@@ -383,7 +383,6 @@ class MultiCurl
|
||||
public function setCookie($key, $value)
|
||||
{
|
||||
$this->cookies[$key] = $value;
|
||||
$this->setOpt(CURLOPT_COOKIE, str_replace('+', '%20', http_build_query($this->cookies, '', '; ')));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -699,6 +698,9 @@ class MultiCurl
|
||||
foreach ($this->headers as $key => $value) {
|
||||
$curl->setHeader($key, $value);
|
||||
}
|
||||
foreach ($this->cookies as $key => $value) {
|
||||
$curl->setCookie($key, $value);
|
||||
}
|
||||
$curl->setJsonDecoder($this->jsonDecoder);
|
||||
$curl->setXmlDecoder($this->xmlDecoder);
|
||||
|
||||
@@ -708,6 +710,7 @@ class MultiCurl
|
||||
}
|
||||
|
||||
$this->activeCurls[$curl->id] = $curl;
|
||||
$this->responseCookies = array();
|
||||
$curl->call($curl->beforeSendFunction);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -877,9 +877,8 @@ class CurlTest extends PHPUnit_Framework_TestCase
|
||||
$test->server('multiple_cookie', 'GET');
|
||||
$this->assertEquals('cookie1=scrumptious,cookie2=mouthwatering', $test->curl->responseHeaders['Set-Cookie']);
|
||||
|
||||
$response_cookies = $test->curl->getResponseCookies();
|
||||
$this->assertEquals('scrumptious', $response_cookies['cookie1']);
|
||||
$this->assertEquals('mouthwatering', $response_cookies['cookie2']);
|
||||
$this->assertEquals('scrumptious', $test->curl->responseCookies['cookie1']);
|
||||
$this->assertEquals('mouthwatering', $test->curl->responseCookies['cookie2']);
|
||||
}
|
||||
|
||||
public function testDefaultTimeout()
|
||||
|
||||
@@ -2001,31 +2001,37 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testCookies()
|
||||
{
|
||||
$data = array('key' => 'mycookie');
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'cookie');
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'setcookie');
|
||||
$multi_curl->setCookie('mycookie', 'yum');
|
||||
$multi_curl->setCookie('cookie-for-all-before', 'a');
|
||||
|
||||
$get_1 = $multi_curl->addGet(Test::TEST_URL, $data);
|
||||
$get_1 = $multi_curl->addGet(Test::TEST_URL);
|
||||
$get_1->setCookie('cookie-for-1st-request', '1');
|
||||
$get_1->complete(function ($instance) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertEquals('yum', $instance->response);
|
||||
PHPUnit_Framework_Assert::assertEquals('yum', $instance->responseCookies['mycookie']);
|
||||
PHPUnit_Framework_Assert::assertEquals('a', $instance->responseCookies['cookie-for-all-before']);
|
||||
PHPUnit_Framework_Assert::assertEquals('b', $instance->responseCookies['cookie-for-all-after']);
|
||||
PHPUnit_Framework_Assert::assertEquals('1', $instance->responseCookies['cookie-for-1st-request']);
|
||||
});
|
||||
|
||||
$get_2 = $multi_curl->addGet(Test::TEST_URL, $data);
|
||||
$get_2 = $multi_curl->addGet(Test::TEST_URL);
|
||||
$get_2->setCookie('cookie-for-2nd-request', '2');
|
||||
$get_2->beforeSend(function ($instance) {
|
||||
$instance->setCookie('mycookie', 'yummy');
|
||||
});
|
||||
$get_2->complete(function ($instance) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertEquals('yummy', $instance->response);
|
||||
PHPUnit_Framework_Assert::assertEquals('yummy', $instance->responseCookies['mycookie']);
|
||||
PHPUnit_Framework_Assert::assertEquals('a', $instance->responseCookies['cookie-for-all-before']);
|
||||
PHPUnit_Framework_Assert::assertEquals('b', $instance->responseCookies['cookie-for-all-after']);
|
||||
PHPUnit_Framework_Assert::assertEquals('2', $instance->responseCookies['cookie-for-2nd-request']);
|
||||
});
|
||||
|
||||
$multi_curl->setCookie('cookie-for-all-after', 'b');
|
||||
$multi_curl->start();
|
||||
|
||||
$this->assertEquals('yum', $get_1->response);
|
||||
$this->assertEquals('yummy', $get_2->response);
|
||||
$this->assertEquals('yum', $get_1->responseCookies['mycookie']);
|
||||
$this->assertEquals('yummy', $get_2->responseCookies['mycookie']);
|
||||
}
|
||||
|
||||
public function testJSONDecoder()
|
||||
|
||||
@@ -143,6 +143,11 @@ if ($test === 'http_basic_auth') {
|
||||
} elseif ($test === 'request_uri') {
|
||||
echo $_SERVER['REQUEST_URI'];
|
||||
exit;
|
||||
} elseif ($test === 'setcookie') {
|
||||
foreach ($_COOKIE as $key => $value) {
|
||||
setcookie($key, $value);
|
||||
}
|
||||
exit;
|
||||
} elseif ($test === 'cookiejar') {
|
||||
setcookie('mycookie', 'yum');
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user