mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-31 20:52:45 +00:00
Implement additional MultiCurl functions available in Curl.
MultiCurl::setBasicAuthentication(), MultiCurl::setCookie(), MultiCurl::setCookieFile(), MultiCurl::setCookieJar(), MultiCurl::setJsonDecoder(), MultiCurl::setReferer(), MultiCurl::setReferrer(), MultiCurl::setTimeout(), MultiCurl::setUserAgent(), MultiCurl::unsetHeader(), MultiCurl::verbose()
This commit is contained in:
+4
-2
@@ -345,9 +345,11 @@ class Curl
|
||||
}, $headers, array_keys($headers)));
|
||||
}
|
||||
|
||||
public function setJsonDecoder($func)
|
||||
public function setJsonDecoder($function)
|
||||
{
|
||||
$this->json_decoder = $func;
|
||||
if (is_callable($function)) {
|
||||
$this->json_decoder = $function;
|
||||
}
|
||||
}
|
||||
|
||||
public function setOpt($option, $value)
|
||||
|
||||
+67
-3
@@ -8,14 +8,17 @@ class MultiCurl
|
||||
public $curls = array();
|
||||
private $curl_fhs = array();
|
||||
|
||||
private $headers = array();
|
||||
private $options = array();
|
||||
|
||||
private $before_send_function = null;
|
||||
private $success_function = null;
|
||||
private $error_function = null;
|
||||
private $complete_function = null;
|
||||
|
||||
private $cookies = array();
|
||||
private $headers = array();
|
||||
private $options = array();
|
||||
|
||||
private $json_decoder = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->multi_curl = curl_multi_init();
|
||||
@@ -143,16 +146,65 @@ class MultiCurl
|
||||
return $this->options[$option];
|
||||
}
|
||||
|
||||
public function setBasicAuthentication($username, $password = '')
|
||||
{
|
||||
$this->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||
$this->setOpt(CURLOPT_USERPWD, $username . ':' . $password);
|
||||
}
|
||||
|
||||
public function setCookie($key, $value)
|
||||
{
|
||||
$this->cookies[$key] = $value;
|
||||
$this->setOpt(CURLOPT_COOKIE, str_replace('+', '%20', http_build_query($this->cookies, '', '; ')));
|
||||
}
|
||||
|
||||
public function setCookieFile($cookie_file)
|
||||
{
|
||||
$this->setOpt(CURLOPT_COOKIEFILE, $cookie_file);
|
||||
}
|
||||
|
||||
public function setCookieJar($cookie_jar)
|
||||
{
|
||||
$this->setOpt(CURLOPT_COOKIEJAR, $cookie_jar);
|
||||
}
|
||||
|
||||
public function setHeader($key, $value)
|
||||
{
|
||||
$this->headers[$key] = $value;
|
||||
}
|
||||
|
||||
public function setJsonDecoder($function)
|
||||
{
|
||||
if (is_callable($function)) {
|
||||
$this->json_decoder = $function;
|
||||
}
|
||||
}
|
||||
|
||||
public function setOpt($option, $value)
|
||||
{
|
||||
$this->options[$option] = $value;
|
||||
}
|
||||
|
||||
public function setReferer($referer)
|
||||
{
|
||||
$this->setReferrer($referer);
|
||||
}
|
||||
|
||||
public function setReferrer($referrer)
|
||||
{
|
||||
$this->setOpt(CURLOPT_REFERER, $referrer);
|
||||
}
|
||||
|
||||
public function setTimeout($seconds)
|
||||
{
|
||||
$this->setOpt(CURLOPT_TIMEOUT, $seconds);
|
||||
}
|
||||
|
||||
public function setUserAgent($user_agent)
|
||||
{
|
||||
$this->setOpt(CURLOPT_USERAGENT, $user_agent);
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
foreach ($this->curls as $ch) {
|
||||
@@ -162,6 +214,7 @@ class MultiCurl
|
||||
foreach ($this->headers as $key => $value) {
|
||||
$ch->setHeader($key, $value);
|
||||
}
|
||||
$ch->setJsonDecoder($this->json_decoder);
|
||||
$ch->call($ch->before_send_function);
|
||||
}
|
||||
|
||||
@@ -200,6 +253,17 @@ class MultiCurl
|
||||
$this->success_function = $callback;
|
||||
}
|
||||
|
||||
public function unsetHeader($key)
|
||||
{
|
||||
$this->setHeader($key, '');
|
||||
unset($this->headers[$key]);
|
||||
}
|
||||
|
||||
public function verbose($on = true)
|
||||
{
|
||||
$this->setOpt(CURLOPT_VERBOSE, $on);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->close();
|
||||
|
||||
@@ -1572,4 +1572,102 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($curl_user_agent, $get_2->getOpt(CURLOPT_USERAGENT));
|
||||
$this->assertEquals($curl_user_agent, $get_2->response);
|
||||
}
|
||||
|
||||
public function testBasicHttpAuthSuccess()
|
||||
{
|
||||
$username1 = 'myusername';
|
||||
$password1 = 'mypassword';
|
||||
$username2 = 'myotherusername';
|
||||
$password2 = 'myotherpassword';
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'http_basic_auth');
|
||||
$multi_curl->setBasicAuthentication($username1, $password1);
|
||||
|
||||
$get_1 = $multi_curl->addGet(Test::TEST_URL);
|
||||
$get_1->complete(function ($instance) use ($username1, $password1) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertEquals($username1, $instance->response->username);
|
||||
PHPUnit_Framework_Assert::assertEquals($password1, $instance->response->password);
|
||||
});
|
||||
|
||||
$get_2 = $multi_curl->addGet(Test::TEST_URL);
|
||||
$get_2->beforeSend(function ($instance) use ($username2, $password2) {
|
||||
$instance->setBasicAuthentication($username2, $password2);
|
||||
});
|
||||
$get_2->complete(function ($instance) use ($username2, $password2) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertEquals($username2, $instance->response->username);
|
||||
PHPUnit_Framework_Assert::assertEquals($password2, $instance->response->password);
|
||||
});
|
||||
|
||||
$multi_curl->start();
|
||||
|
||||
$this->assertEquals(CURLAUTH_BASIC, $multi_curl->getOpt(CURLOPT_HTTPAUTH));
|
||||
$this->assertEquals(CURLAUTH_BASIC, $get_1->getOpt(CURLOPT_HTTPAUTH));
|
||||
$this->assertEquals($username1, $get_1->response->username);
|
||||
$this->assertEquals($password1, $get_1->response->password);
|
||||
$this->assertEquals(CURLAUTH_BASIC, $get_2->getOpt(CURLOPT_HTTPAUTH));
|
||||
$this->assertEquals($username2, $get_2->response->username);
|
||||
$this->assertEquals($password2, $get_2->response->password);
|
||||
}
|
||||
|
||||
public function testCookies()
|
||||
{
|
||||
$data = array('key' => 'mycookie');
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'cookie');
|
||||
$multi_curl->setCookie('mycookie', 'yum');
|
||||
|
||||
$get_1 = $multi_curl->addGet(Test::TEST_URL, $data);
|
||||
$get_1->complete(function ($instance) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertEquals('yum', $instance->response);
|
||||
});
|
||||
|
||||
$get_2 = $multi_curl->addGet(Test::TEST_URL, $data);
|
||||
$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);
|
||||
});
|
||||
|
||||
$multi_curl->start();
|
||||
|
||||
$this->assertEquals('yum', $get_1->response);
|
||||
$this->assertEquals('yummy', $get_2->response);
|
||||
}
|
||||
|
||||
public function testJSONDecoder()
|
||||
{
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'json_response');
|
||||
$multi_curl->setJsonDecoder(function($response) {
|
||||
return 'foo';
|
||||
});
|
||||
|
||||
$get_1 = $multi_curl->addGet(Test::TEST_URL);
|
||||
$get_1->complete(function ($instance) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertEquals('foo', $instance->response);
|
||||
});
|
||||
|
||||
$get_2 = $multi_curl->addGet(Test::TEST_URL);
|
||||
$get_2->beforeSend(function ($instance) {
|
||||
$instance->setJsonDecoder(function($response) {
|
||||
return 'bar';
|
||||
});
|
||||
});
|
||||
$get_2->complete(function ($instance) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertEquals('bar', $instance->response);
|
||||
});
|
||||
|
||||
$multi_curl->start();
|
||||
$this->assertEquals('foo', $get_1->response);
|
||||
$this->assertEquals('bar', $get_2->response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,9 +90,13 @@ if ($test == 'http_basic_auth') {
|
||||
echo 'OK';
|
||||
exit;
|
||||
} elseif ($test === 'json_response') {
|
||||
$key = $_POST['key'];
|
||||
$value = $_POST['value'];
|
||||
header($key . ': ' . $value);
|
||||
if ($request_method === 'POST') {
|
||||
$key = $_POST['key'];
|
||||
$value = $_POST['value'];
|
||||
header($key . ': ' . $value);
|
||||
} else {
|
||||
header('Content-Type: application/json');
|
||||
}
|
||||
echo json_encode(array(
|
||||
'null' => null,
|
||||
'true' => true,
|
||||
|
||||
Reference in New Issue
Block a user