From 2098e741237f0ec165c19ede200b156670a40942 Mon Sep 17 00:00:00 2001 From: Roman Shamyan Date: Tue, 7 Jul 2015 15:16:08 +0300 Subject: [PATCH 1/4] autoload --- EpiCurl.php | 16 +++++++++++----- EpiOAuth.php | 5 +++++ EpiSequence.php | 3 +++ EpiTwitter.php | 9 +++++++++ composer.json | 7 ++++++- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/EpiCurl.php b/EpiCurl.php index 37d2aa5..5e5cbc1 100644 --- a/EpiCurl.php +++ b/EpiCurl.php @@ -1,14 +1,19 @@ execStatus = curl_multi_exec($this->mc, $this->running); + //$code = $this->execStatus = curl_multi_exec($this->mc, $this->running); } while ($this->execStatus === CURLM_CALL_MULTI_PERFORM); return new EpiCurlManager($key); @@ -96,7 +101,7 @@ public function getResult($key = null) { return $this->responses[$key]; } - $runningCurrent = $this->running; + //$runningCurrent = $this->running; } return null; } @@ -118,7 +123,7 @@ private function getKey($ch) return (string)$ch; } - private function headerCallback($ch, $header) + /*private function headerCallback($ch, $header) { $_header = trim($header); $colonPos= strpos($_header, ':'); @@ -130,6 +135,7 @@ private function headerCallback($ch, $header) } return strlen($header); } + */ private function storeResponses() { diff --git a/EpiOAuth.php b/EpiOAuth.php index 6831b33..3bd5112 100644 --- a/EpiOAuth.php +++ b/EpiOAuth.php @@ -1,4 +1,9 @@ */ + +namespace jmathai\twitter_async; + +use ArrayAccess; +use ArrayIterator; +use Countable; +use Exception; +use IteratorAggregate; + class EpiTwitter extends EpiOAuth { const EPITWITTER_SIGNATURE_METHOD = 'HMAC-SHA1'; diff --git a/composer.json b/composer.json index da4d8d5..23effbd 100644 --- a/composer.json +++ b/composer.json @@ -3,5 +3,10 @@ "description": "Twitter-async is a high performance wrapper for Twitter's OAuth API which provides parallel/asynchronous calls.", "homepage": "http://www.jaisenmathai.com/articles/twitter-async-documentation.html", "author": "Jaisen Mathai", - "license": "BSD" + "license": "BSD", + "autoload": { + "psr-4": { + "jmathai\\twitter_async\\": "" + } + } } \ No newline at end of file From c875f6871f1feb3ee53bcd4c8429c20a3e8810ea Mon Sep 17 00:00:00 2001 From: Roman Shamyan Date: Wed, 29 Jul 2015 14:29:51 +0300 Subject: [PATCH 2/4] revetted commented code EpiCurl.php --- EpiCurl.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/EpiCurl.php b/EpiCurl.php index 5e5cbc1..dd77489 100644 --- a/EpiCurl.php +++ b/EpiCurl.php @@ -10,10 +10,10 @@ class EpiCurl static $inst = null; static $singleton = 0; private $mc; - //private $msgs; + private $msgs; private $running; private $execStatus; - //private $selectStatus; + private $selectStatus; private $sleepIncrement = 1.1; private $requests = array(); private $responses = array(); @@ -61,7 +61,7 @@ public function addCurl($ch) if($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM) { do { - //$code = $this->execStatus = curl_multi_exec($this->mc, $this->running); + $code = $this->execStatus = curl_multi_exec($this->mc, $this->running); } while ($this->execStatus === CURLM_CALL_MULTI_PERFORM); return new EpiCurlManager($key); From 6293f9fafd8816c145c10ebb69103eececdadd5f Mon Sep 17 00:00:00 2001 From: Roman Shamyan Date: Wed, 29 Jul 2015 14:32:22 +0300 Subject: [PATCH 3/4] amending previous --- EpiCurl.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/EpiCurl.php b/EpiCurl.php index dd77489..c1f7b97 100644 --- a/EpiCurl.php +++ b/EpiCurl.php @@ -101,7 +101,7 @@ public function getResult($key = null) { return $this->responses[$key]; } - //$runningCurrent = $this->running; + $runningCurrent = $this->running; } return null; } @@ -123,7 +123,7 @@ private function getKey($ch) return (string)$ch; } - /*private function headerCallback($ch, $header) + private function headerCallback($ch, $header) { $_header = trim($header); $colonPos= strpos($_header, ':'); @@ -135,7 +135,6 @@ private function getKey($ch) } return strlen($header); } - */ private function storeResponses() { From 970f2e0ca8bc6b1e8159f3187ffdb022f175a23e Mon Sep 17 00:00:00 2001 From: Roman Shamyan Date: Wed, 29 Jul 2015 20:31:25 +0300 Subject: [PATCH 4/4] put EpiCurl to require in composer.json --- .gitignore | 3 + EpiCurl.php | 217 ------------------------------------------------- EpiOAuth.php | 2 + EpiTwitter.php | 1 + composer.json | 27 ++++-- simpleTest.php | 11 ++- 6 files changed, 32 insertions(+), 229 deletions(-) delete mode 100644 EpiCurl.php diff --git a/.gitignore b/.gitignore index edc3de6..6dd4c78 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ Thumbs.db __MACOSX # Project Specific +/vendor/ +composer.lock +.idea/ diff --git a/EpiCurl.php b/EpiCurl.php deleted file mode 100644 index c1f7b97..0000000 --- a/EpiCurl.php +++ /dev/null @@ -1,217 +0,0 @@ -mc = curl_multi_init(); - $this->properties = array( - 'code' => CURLINFO_HTTP_CODE, - 'time' => CURLINFO_TOTAL_TIME, - 'length'=> CURLINFO_CONTENT_LENGTH_DOWNLOAD, - 'type' => CURLINFO_CONTENT_TYPE, - 'url' => CURLINFO_EFFECTIVE_URL - ); - } - - public function addEasyCurl($ch) - { - $key = $this->getKey($ch); - $this->requests[$key] = $ch; - curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'headerCallback')); - $done = array('handle' => $ch); - $this->storeResponse($done, false); - $this->startTimer($key); - return new EpiCurlManager($key); - } - - public function addCurl($ch) - { - $key = $this->getKey($ch); - $this->requests[$key] = $ch; - curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'headerCallback')); - - $code = curl_multi_add_handle($this->mc, $ch); - $this->startTimer($key); - - // (1) - if($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM) - { - do { - $code = $this->execStatus = curl_multi_exec($this->mc, $this->running); - } while ($this->execStatus === CURLM_CALL_MULTI_PERFORM); - - return new EpiCurlManager($key); - } - else - { - return $code; - } - } - - public function getResult($key = null) - { - if($key != null) - { - if(isset($this->responses[$key])) - { - return $this->responses[$key]; - } - - $innerSleepInt = $outerSleepInt = 1; - while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) - { - usleep(intval($outerSleepInt)); - $outerSleepInt = intval(max(1, ($outerSleepInt*$this->sleepIncrement))); - $ms=curl_multi_select($this->mc, 0); - if($ms > 0) - { - do{ - $this->execStatus = curl_multi_exec($this->mc, $this->running); - usleep(intval($innerSleepInt)); - $innerSleepInt = intval(max(1, ($innerSleepInt*$this->sleepIncrement))); - }while($this->execStatus==CURLM_CALL_MULTI_PERFORM); - $innerSleepInt = 1; - } - $this->storeResponses(); - if(isset($this->responses[$key]['data'])) - { - return $this->responses[$key]; - } - $runningCurrent = $this->running; - } - return null; - } - return false; - } - - public static function getSequence() - { - return new EpiSequence(self::$timers); - } - - public static function getTimers() - { - return self::$timers; - } - - private function getKey($ch) - { - return (string)$ch; - } - - private function headerCallback($ch, $header) - { - $_header = trim($header); - $colonPos= strpos($_header, ':'); - if($colonPos > 0) - { - $key = substr($_header, 0, $colonPos); - $val = preg_replace('/^\W+/','',substr($_header, $colonPos)); - $this->responses[$this->getKey($ch)]['headers'][$key] = $val; - } - return strlen($header); - } - - private function storeResponses() - { - while($done = curl_multi_info_read($this->mc)) - { - $this->storeResponse($done); - } - } - - private function storeResponse($done, $isAsynchronous = true) - { - $key = $this->getKey($done['handle']); - $this->stopTimer($key, $done); - if($isAsynchronous) - $this->responses[$key]['data'] = curl_multi_getcontent($done['handle']); - else - $this->responses[$key]['data'] = curl_exec($done['handle']); - - foreach($this->properties as $name => $const) - { - $this->responses[$key][$name] = curl_getinfo($done['handle'], $const); - } - if($isAsynchronous) - curl_multi_remove_handle($this->mc, $done['handle']); - curl_close($done['handle']); - } - - private function startTimer($key) - { - self::$timers[$key]['start'] = microtime(true); - } - - private function stopTimer($key, $done) - { - self::$timers[$key]['end'] = microtime(true); - self::$timers[$key]['api'] = curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL); - self::$timers[$key]['time'] = curl_getinfo($done['handle'], CURLINFO_TOTAL_TIME); - self::$timers[$key]['code'] = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE); - } - - static function getInstance() - { - if(self::$inst == null) - { - self::$singleton = 1; - self::$inst = new EpiCurl(); - } - - return self::$inst; - } -} - -class EpiCurlManager -{ - private $key; - private $epiCurl; - - public function __construct($key) - { - $this->key = $key; - $this->epiCurl = EpiCurl::getInstance(); - } - - public function __get($name) - { - $responses = $this->epiCurl->getResult($this->key); - return isset($responses[$name]) ? $responses[$name] : null; - } - - public function __isset($name) - { - $val = self::__get($name); - return empty($val); - } -} - -/* - * Credits: - * - (1) Alistair pointed out that curl_multi_add_handle can return CURLM_CALL_MULTI_PERFORM on success. - */ diff --git a/EpiOAuth.php b/EpiOAuth.php index 3bd5112..bb93136 100644 --- a/EpiOAuth.php +++ b/EpiOAuth.php @@ -2,6 +2,8 @@ namespace jmathai\twitter_async; +use jmathai\php_multi_curl\EpiCurl; + use Exception; class EpiOAuth diff --git a/EpiTwitter.php b/EpiTwitter.php index 3126165..d6b5869 100644 --- a/EpiTwitter.php +++ b/EpiTwitter.php @@ -17,6 +17,7 @@ use Countable; use Exception; use IteratorAggregate; +use jmathai\php_multi_curl\EpiCurl; class EpiTwitter extends EpiOAuth { diff --git a/composer.json b/composer.json index 23effbd..c0787c0 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,21 @@ { - "name": "jmathai/twitter-async", - "description": "Twitter-async is a high performance wrapper for Twitter's OAuth API which provides parallel/asynchronous calls.", - "homepage": "http://www.jaisenmathai.com/articles/twitter-async-documentation.html", - "author": "Jaisen Mathai", - "license": "BSD", - "autoload": { - "psr-4": { - "jmathai\\twitter_async\\": "" - } + "name": "jmathai/twitter-async", + "description": "Twitter-async is a high performance wrapper for Twitter's OAuth API which provides parallel/asynchronous calls.", + "homepage": "http://www.jaisenmathai.com/articles/twitter-async-documentation.html", + "author": "Jaisen Mathai", + "license": "BSD", + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/jmathai/php-multi-curl" } + ], + "require": { + "jmathai/php-multi-curl": ">= 0.1" + }, + "autoload": { + "psr-4": { + "jmathai\\twitter_async\\": "" + } + } } \ No newline at end of file diff --git a/simpleTest.php b/simpleTest.php index 8dfc664..7b122b8 100644 --- a/simpleTest.php +++ b/simpleTest.php @@ -1,7 +1,12 @@