mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 04:01:47 +00:00
Implement MultiCurl::setHeader(), MultiCurl::setOpt(), MultiCurl::getOpt()
This commit is contained in:
@@ -7,6 +7,9 @@ class MultiCurl
|
||||
public $multi_curl;
|
||||
public $curls = array();
|
||||
|
||||
private $headers = array();
|
||||
private $options = array();
|
||||
|
||||
private $before_send_function = null;
|
||||
private $success_function = null;
|
||||
private $error_function = null;
|
||||
@@ -15,6 +18,7 @@ class MultiCurl
|
||||
public function __construct()
|
||||
{
|
||||
$this->multi_curl = curl_multi_init();
|
||||
$this->headers = new CaseInsensitiveArray();
|
||||
}
|
||||
|
||||
public function addDelete($url, $data = array())
|
||||
@@ -124,12 +128,42 @@ class MultiCurl
|
||||
$this->complete_function = $callback;
|
||||
}
|
||||
|
||||
public function setHeader($key, $value)
|
||||
{
|
||||
$this->headers[$key] = $value;
|
||||
}
|
||||
|
||||
public function setOpt($option, $value)
|
||||
{
|
||||
$this->options[$option] = $value;
|
||||
}
|
||||
|
||||
public function getOpt($option)
|
||||
{
|
||||
return $this->options[$option];
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
echo 'running start' . "\n";
|
||||
foreach ($this->curls as $ch) {
|
||||
echo 'next curl' . "\n";
|
||||
|
||||
echo 'setting options for curl' . "\n";
|
||||
foreach ($this->options as $option => $value) {
|
||||
$ch->setOpt($option, $value);
|
||||
}
|
||||
echo 'options set for curl' . "\n";
|
||||
|
||||
echo 'setting headers for curl' . "\n";
|
||||
foreach ($this->headers as $key => $value) {
|
||||
$ch->setHeader($key, $value);
|
||||
}
|
||||
echo 'headers set for curl' . "\n";
|
||||
|
||||
echo 'about to call before send' . "\n";
|
||||
$ch->call($ch->before_send_function);
|
||||
echo 'before send called' . "\n";
|
||||
}
|
||||
echo 'called all before sends' . "\n";
|
||||
|
||||
|
||||
@@ -1352,4 +1352,40 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($put_error_called);
|
||||
$this->assertTrue($put_complete_called);
|
||||
}
|
||||
|
||||
public function testSetOptAndSetOptOverride()
|
||||
{
|
||||
$multi_curl_user_agent = 'multi curl user agent';
|
||||
$curl_user_agent = 'curl user agent';
|
||||
$data = array('key' => 'HTTP_USER_AGENT');
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'server');
|
||||
$multi_curl->setOpt(CURLOPT_USERAGENT, $multi_curl_user_agent);
|
||||
|
||||
$get_1 = $multi_curl->addGet(Test::TEST_URL, $data);
|
||||
$get_1->complete(function ($instance) use ($multi_curl_user_agent) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertEquals($multi_curl_user_agent, $instance->getOpt(CURLOPT_USERAGENT));
|
||||
PHPUnit_Framework_Assert::assertEquals($multi_curl_user_agent, $instance->response);
|
||||
});
|
||||
|
||||
$get_2 = $multi_curl->addGet(Test::TEST_URL, $data);
|
||||
$get_2->beforeSend(function ($instance) use ($curl_user_agent) {
|
||||
$instance->setOpt(CURLOPT_USERAGENT, $curl_user_agent);
|
||||
});
|
||||
$get_2->complete(function ($instance) use ($curl_user_agent) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertEquals($curl_user_agent, $instance->getOpt(CURLOPT_USERAGENT));
|
||||
PHPUnit_Framework_Assert::assertEquals($curl_user_agent, $instance->response);
|
||||
});
|
||||
|
||||
$multi_curl->start();
|
||||
|
||||
$this->assertEquals($multi_curl_user_agent, $multi_curl->getOpt(CURLOPT_USERAGENT));
|
||||
$this->assertEquals($multi_curl_user_agent, $get_1->getOpt(CURLOPT_USERAGENT));
|
||||
$this->assertEquals($multi_curl_user_agent, $get_1->response);
|
||||
$this->assertEquals($curl_user_agent, $get_2->getOpt(CURLOPT_USERAGENT));
|
||||
$this->assertEquals($curl_user_agent, $get_2->response);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user