├── .gitignore ├── src ├── Exception.php ├── Result.php └── Curl.php ├── README.md ├── composer.json └── example └── simple.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /src/Exception.php: -------------------------------------------------------------------------------- 1 | result = $result; 14 | parent::__construct($result->getError(), $result->getErrorCode()); 15 | } 16 | } -------------------------------------------------------------------------------- /src/Result.php: -------------------------------------------------------------------------------- 1 | query['params']['__deferred']; 14 | unset($this->query['params']['__deferred']); 15 | return $deferred; 16 | } 17 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | reactphp-curl 2 | =============== 3 | 4 | ## Install 5 | 6 | The recommended way to install reactphp-curl is through [composer](http://getcomposer.org). 7 | 8 | ``` 9 | { 10 | "require": { 11 | "khr/react-curl": "*" 12 | } 13 | } 14 | ``` 15 | 16 | ## Introduction 17 | 18 | This is a multi curl driver for [reactphp](https://github.com/reactphp/react) 19 | 20 | Example show in folder /example/ 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "khr/react-curl", 3 | "description": "multi curl driver for reactphp.", 4 | "keywords": ["mysql", "php", "multi", "curl"], 5 | "license": "None", 6 | "require": { 7 | "php": ">=5.4.0", 8 | 9 | "react/promise": "~2.0", 10 | "react/event-loop": "*", 11 | "khr/php-mcurl-client": "3.*" 12 | }, 13 | "require-dev": { 14 | 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "KHR\\React\\Curl\\": "src" 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /example/simple.php: -------------------------------------------------------------------------------- 1 | info['url'], PHP_EOL; 11 | //print_r($result->info); 12 | }; 13 | 14 | $cb_err = function(Exception $e){ 15 | echo $e->result->info['url'], "\t", $e->getMessage(), PHP_EOL; 16 | }; 17 | 18 | 19 | $loop = Factory::create(); 20 | $curl = new Curl($loop); 21 | 22 | // Config 23 | $curl->client->setMaxRequest(3); 24 | $curl->client->setSleep(6, 1.0, false); // 6 request in 1 second 25 | $curl->client->setCurlOption([CURLOPT_AUTOREFERER => true, CURLOPT_COOKIE => 'fruit=apple; colour=red']); // default options 26 | 27 | // More config $curl->client show https://github.com/KhristenkoYura/multicurl 28 | // endconfig 29 | 30 | //get result 31 | $curl->get('https://graph.facebook.com/http://www.yandex.ru')->then(function($result){ 32 | echo (string) $result, PHP_EOL; // echo $result->body; OR echo $result->getBody(); 33 | }); 34 | 35 | // get json 36 | $curl->get('https://graph.facebook.com/http://google.com')->then(function($result){ 37 | print_r($result->json); 38 | // print_r($result->getJson(true)); -> print array 39 | }); 40 | 41 | $curl->get('https://www.google.ru/')->then($cb_ok, $cb_err); 42 | $curl->get('http://www.yandex.ru/', [CURLOPT_REFERER => 'https://google.ru/'])->then($cb_ok, $cb_err); 43 | 44 | $curl->get('http://yandex.ru/404.html', [CURLOPT_REFERER => 'https://google.ru/'])->then($cb_ok, $cb_err); 45 | 46 | 47 | 48 | $curl->add([CURLOPT_URL => 'http://www.yandex.ru/'])->then($cb_ok, $cb_err); 49 | $curl->add([CURLOPT_URL => 'https://google.ru/'])->then($cb_ok, $cb_err); 50 | //Run request in method add 51 | $curl->run(); 52 | 53 | $curl->post('http://www.yandex.ru/post', ['post-key' => 'post-value'])->then($cb_ok, $cb_err); 54 | 55 | 56 | $loop->run(); 57 | -------------------------------------------------------------------------------- /src/Curl.php: -------------------------------------------------------------------------------- 1 | loop = $loop; 36 | $this->client = new \MCurl\Client(); 37 | $this->client->isSelect(false); 38 | $this->client->setClassResult('\\KHR\\React\Curl\\Result'); 39 | } 40 | 41 | /** 42 | * @param $url 43 | * @param array $opts 44 | * @return Promise 45 | */ 46 | public function get($url, $opts = array()) { 47 | $opts[CURLOPT_URL] = $url; 48 | $promise = $this->add($opts); 49 | $this->run(); 50 | return $promise; 51 | } 52 | 53 | /** 54 | * @param $url 55 | * @param array $data 56 | * @param array $opts 57 | * @return Promise 58 | */ 59 | public function post($url, $data = array(), $opts = array()) { 60 | $opts[CURLOPT_POST] = true; 61 | $opts[CURLOPT_POSTFIELDS] = $data; 62 | return $this->get($url, $opts); 63 | } 64 | 65 | /** 66 | * @param $opts 67 | * @param array $params 68 | * @return Promise 69 | */ 70 | public function add($opts, $params = []) { 71 | $params['__deferred'] = $deferred = new Deferred(); 72 | $this->client->add($opts, $params); 73 | return $deferred->promise(); 74 | } 75 | 76 | public function run() { 77 | $client = $this->client; 78 | $client->run(); 79 | 80 | while($client->has()) { 81 | /** 82 | * @var Result $result 83 | */ 84 | $result = $client->next(); 85 | $deferred = $result->shiftDeferred(); 86 | 87 | if (!$result->hasError()) { 88 | $deferred->resolve($result); 89 | } else { 90 | $deferred->reject(new Exception($result)); 91 | } 92 | } 93 | 94 | if (!isset($this->loop_timer)) { 95 | $this->loop_timer = $this->loop->addPeriodicTimer($this->timeout, function() { 96 | $this->run(); 97 | if (!($this->client->run() || $this->client->has())) { 98 | $this->loop_timer->cancel(); 99 | $this->loop_timer = null; 100 | } 101 | }); 102 | } 103 | } 104 | } 105 | --------------------------------------------------------------------------------