mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-26 07:49:23 +00:00
Implement success, error, and complete callbacks
This commit is contained in:
+16
-4
@@ -76,7 +76,7 @@ class Curl {
|
||||
else {
|
||||
$this->setopt(CURLOPT_URL, $this->_buildURL($url_mixed, $data));
|
||||
$this->setopt(CURLOPT_HTTPGET, TRUE);
|
||||
return $this->_exec($this);
|
||||
return $this->_exec();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,6 +176,18 @@ class Curl {
|
||||
return implode('&', $query);
|
||||
}
|
||||
|
||||
public function success($callback) {
|
||||
$this->_success = $callback;
|
||||
}
|
||||
|
||||
public function error($callback) {
|
||||
$this->_error = $callback;
|
||||
}
|
||||
|
||||
public function complete($callback) {
|
||||
$this->_complete = $callback;
|
||||
}
|
||||
|
||||
private function _buildURL($url, $data=array()) {
|
||||
return $url . (empty($data) ? '' : '?' . http_build_query($data));
|
||||
}
|
||||
@@ -232,13 +244,13 @@ class Curl {
|
||||
$ch->error_message = $ch->curl_error ? $ch->curl_error_message : $ch->http_error_message;
|
||||
|
||||
if (!$ch->error) {
|
||||
$ch->_call($ch->_success);
|
||||
$ch->_call($ch->_success, $ch);
|
||||
}
|
||||
else {
|
||||
$ch->_call($ch->_error);
|
||||
$ch->_call($ch->_error, $ch);
|
||||
}
|
||||
|
||||
$ch->_call($ch->_complete);
|
||||
$ch->_call($ch->_complete, $ch);
|
||||
|
||||
return $ch->error_code;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
class Test {
|
||||
const TEST_URL = 'https://127.0.0.1/php-curl-class/tests/server.php';
|
||||
const ERROR_URL = 'https://1.2.3.4/';
|
||||
|
||||
function __construct() {
|
||||
$this->curl = new Curl();
|
||||
|
||||
+40
-1
@@ -167,7 +167,7 @@ class CurlTest extends PHPUnit_Framework_TestCase {
|
||||
public function testError() {
|
||||
$test = new Test();
|
||||
$test->curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000);
|
||||
$test->curl->get('http://1.2.3.4/');
|
||||
$test->curl->get(Test::ERROR_URL);
|
||||
$this->assertTrue($test->curl->error === TRUE);
|
||||
$this->assertTrue($test->curl->curl_error === TRUE);
|
||||
$this->assertTrue($test->curl->curl_error_code === CURLE_OPERATION_TIMEOUTED);
|
||||
@@ -297,4 +297,43 @@ class CurlTest extends PHPUnit_Framework_TestCase {
|
||||
$this->assertTrue(substr($curl->curls['1']->response, - $len) === '/b/?foo=bar');
|
||||
$this->assertTrue(substr($curl->curls['2']->response, - $len) === '/c/?foo=bar');
|
||||
}
|
||||
|
||||
public function testSuccessCallback() {
|
||||
$success_called = FALSE;
|
||||
$error_called = FALSE;
|
||||
$complete_called = FALSE;
|
||||
|
||||
$curl = new Curl();
|
||||
$curl->setHeader('X-DEBUG-TEST', 'get');
|
||||
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
|
||||
$curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE);
|
||||
|
||||
$curl->success(function($instance) use (&$success_called, &$error_called, &$complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$success_called = TRUE;
|
||||
});
|
||||
$curl->error(function($instance) use (&$success_called, &$error_called, &$complete_called, &$curl) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertFalse($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$error_called = TRUE;
|
||||
});
|
||||
$curl->complete(function($instance) use (&$success_called, &$error_called, &$complete_called) {
|
||||
PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance);
|
||||
PHPUnit_Framework_Assert::assertTrue($success_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($error_called);
|
||||
PHPUnit_Framework_Assert::assertFalse($complete_called);
|
||||
$complete_called = TRUE;
|
||||
});
|
||||
|
||||
$curl->get(Test::TEST_URL);
|
||||
|
||||
$this->assertTrue($success_called);
|
||||
$this->assertFalse($error_called);
|
||||
$this->assertTrue($complete_called);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user