@@ -6,60 +6,60 @@ discard block |
||
| 6 | 6 | */ |
| 7 | 7 | class MultiCurlSequence |
| 8 | 8 | { |
| 9 | - private $width = 100; |
|
| 10 | - private $timers; |
|
| 11 | - private $min; |
|
| 12 | - private $max; |
|
| 13 | - private $range; |
|
| 14 | - private $step; |
|
| 9 | + private $width = 100; |
|
| 10 | + private $timers; |
|
| 11 | + private $min; |
|
| 12 | + private $max; |
|
| 13 | + private $range; |
|
| 14 | + private $step; |
|
| 15 | 15 | |
| 16 | - public function __construct($timers) |
|
| 17 | - { |
|
| 16 | + public function __construct($timers) |
|
| 17 | + { |
|
| 18 | 18 | $this->timers = $timers; |
| 19 | 19 | |
| 20 | 20 | $min = PHP_INT_MAX; |
| 21 | 21 | $max = 0; |
| 22 | 22 | foreach($this->timers as $timer) |
| 23 | 23 | { |
| 24 | - if(!isset($timer['start'])) |
|
| 24 | + if(!isset($timer['start'])) |
|
| 25 | 25 | $timer['start'] = PHP_INT_MAX; |
| 26 | 26 | |
| 27 | - if(!isset($timer['end'])) |
|
| 27 | + if(!isset($timer['end'])) |
|
| 28 | 28 | $timer['end'] = 0; |
| 29 | 29 | |
| 30 | - $min = min($timer['start'], $min); |
|
| 31 | - $max = max($timer['end'], $max); |
|
| 30 | + $min = min($timer['start'], $min); |
|
| 31 | + $max = max($timer['end'], $max); |
|
| 32 | 32 | } |
| 33 | 33 | $this->min = $min; |
| 34 | 34 | $this->max = $max; |
| 35 | 35 | $this->range = $max-$min; |
| 36 | 36 | $this->step = floatval($this->range/$this->width); |
| 37 | - } |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - public function renderAscii() |
|
| 40 | - { |
|
| 39 | + public function renderAscii() |
|
| 40 | + { |
|
| 41 | 41 | $tpl = ''; |
| 42 | 42 | foreach($this->timers as $timer) |
| 43 | - $tpl .= $this->tplAscii($timer); |
|
| 43 | + $tpl .= $this->tplAscii($timer); |
|
| 44 | 44 | |
| 45 | 45 | return $tpl; |
| 46 | - } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - private function tplAscii($timer) |
|
| 49 | - { |
|
| 48 | + private function tplAscii($timer) |
|
| 49 | + { |
|
| 50 | 50 | $lpad = $rpad = 0; |
| 51 | 51 | $lspace = $chars = $rspace = ''; |
| 52 | 52 | if($timer['start'] > $this->min) |
| 53 | - $lpad = intval(($timer['start'] - $this->min) / $this->step); |
|
| 53 | + $lpad = intval(($timer['start'] - $this->min) / $this->step); |
|
| 54 | 54 | if($timer['end'] < $this->max) |
| 55 | - $rpad = intval(($this->max - $timer['end']) / $this->step); |
|
| 55 | + $rpad = intval(($this->max - $timer['end']) / $this->step); |
|
| 56 | 56 | $mpad = $this->width - $lpad - $rpad; |
| 57 | 57 | if($lpad > 0) |
| 58 | - $lspace = str_repeat(' ', $lpad); |
|
| 58 | + $lspace = str_repeat(' ', $lpad); |
|
| 59 | 59 | if($mpad > 0) |
| 60 | - $chars = str_repeat('=', $mpad); |
|
| 60 | + $chars = str_repeat('=', $mpad); |
|
| 61 | 61 | if($rpad > 0) |
| 62 | - $rspace = str_repeat(' ', $rpad); |
|
| 62 | + $rspace = str_repeat(' ', $rpad); |
|
| 63 | 63 | |
| 64 | 64 | $tpl = <<<TPL |
| 65 | 65 | ({$timer['api']} :: code={$timer['code']}, start={$timer['start']}, end={$timer['end']}, total={$timer['time']}) |
@@ -67,5 +67,5 @@ discard block |
||
| 67 | 67 | |
| 68 | 68 | TPL; |
| 69 | 69 | return $tpl; |
| 70 | - } |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | #!/usr/bin/env php |
| 2 | 2 | <?php |
| 3 | 3 | if (php_sapi_name() !== 'cli') |
| 4 | - die(); |
|
| 4 | + die(); |
|
| 5 | 5 | |
| 6 | 6 | require '../vendor/autoload.php'; |
| 7 | 7 | $mc = JMathai\PhpMultiCurl\MultiCurl::getInstance(); |
@@ -13,39 +13,39 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | class MultiCurl |
| 15 | 15 | { |
| 16 | - const timeout = 3; |
|
| 17 | - private static $inst = null; |
|
| 18 | - /* @TODO make this private and add a method to set it to 0 */ |
|
| 19 | - public static $singleton = 0; |
|
| 20 | - |
|
| 21 | - private $mc; |
|
| 22 | - private $running; |
|
| 23 | - private $execStatus; |
|
| 24 | - private $sleepIncrement = 1.1; |
|
| 25 | - private $requests = array(); |
|
| 26 | - private $responses = array(); |
|
| 27 | - private $properties = array(); |
|
| 28 | - private static $timers = array(); |
|
| 29 | - |
|
| 30 | - public function __construct() |
|
| 31 | - { |
|
| 16 | + const timeout = 3; |
|
| 17 | + private static $inst = null; |
|
| 18 | + /* @TODO make this private and add a method to set it to 0 */ |
|
| 19 | + public static $singleton = 0; |
|
| 20 | + |
|
| 21 | + private $mc; |
|
| 22 | + private $running; |
|
| 23 | + private $execStatus; |
|
| 24 | + private $sleepIncrement = 1.1; |
|
| 25 | + private $requests = array(); |
|
| 26 | + private $responses = array(); |
|
| 27 | + private $properties = array(); |
|
| 28 | + private static $timers = array(); |
|
| 29 | + |
|
| 30 | + public function __construct() |
|
| 31 | + { |
|
| 32 | 32 | if(self::$singleton === 0) |
| 33 | 33 | { |
| 34 | - throw new MultiCurlException('This class cannot be instantiated by the new keyword. You must instantiate it using: $obj = MultiCurl::getInstance();'); |
|
| 34 | + throw new MultiCurlException('This class cannot be instantiated by the new keyword. You must instantiate it using: $obj = MultiCurl::getInstance();'); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | $this->mc = curl_multi_init(); |
| 38 | 38 | $this->properties = array( |
| 39 | - 'code' => CURLINFO_HTTP_CODE, |
|
| 40 | - 'time' => CURLINFO_TOTAL_TIME, |
|
| 41 | - 'length'=> CURLINFO_CONTENT_LENGTH_DOWNLOAD, |
|
| 42 | - 'type' => CURLINFO_CONTENT_TYPE, |
|
| 43 | - 'url' => CURLINFO_EFFECTIVE_URL |
|
| 44 | - ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function addUrl($url, $options = array()) |
|
| 48 | - { |
|
| 39 | + 'code' => CURLINFO_HTTP_CODE, |
|
| 40 | + 'time' => CURLINFO_TOTAL_TIME, |
|
| 41 | + 'length'=> CURLINFO_CONTENT_LENGTH_DOWNLOAD, |
|
| 42 | + 'type' => CURLINFO_CONTENT_TYPE, |
|
| 43 | + 'url' => CURLINFO_EFFECTIVE_URL |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function addUrl($url, $options = array()) |
|
| 48 | + { |
|
| 49 | 49 | $ch = curl_init($url); |
| 50 | 50 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 51 | 51 | foreach($options as $option=>$value) |
@@ -53,13 +53,13 @@ discard block |
||
| 53 | 53 | curl_setopt($ch, $option, $value); |
| 54 | 54 | } |
| 55 | 55 | return $this->addCurl($ch); |
| 56 | - } |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function addCurl($ch) |
|
| 59 | - { |
|
| 58 | + public function addCurl($ch) |
|
| 59 | + { |
|
| 60 | 60 | if(gettype($ch) !== 'resource') |
| 61 | 61 | { |
| 62 | - throw new MultiCurlInvalidParameterException('Parameter must be a valid curl handle'); |
|
| 62 | + throw new MultiCurlInvalidParameterException('Parameter must be a valid curl handle'); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | $key = $this->getKey($ch); |
@@ -72,31 +72,31 @@ discard block |
||
| 72 | 72 | // (1) |
| 73 | 73 | if($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM) |
| 74 | 74 | { |
| 75 | - do |
|
| 76 | - { |
|
| 75 | + do |
|
| 76 | + { |
|
| 77 | 77 | $this->execStatus = curl_multi_exec($this->mc, $this->running); |
| 78 | - } while ($this->execStatus === CURLM_CALL_MULTI_PERFORM); |
|
| 78 | + } while ($this->execStatus === CURLM_CALL_MULTI_PERFORM); |
|
| 79 | 79 | |
| 80 | - return new MultiCurlManager($key); |
|
| 80 | + return new MultiCurlManager($key); |
|
| 81 | 81 | } |
| 82 | 82 | else |
| 83 | 83 | { |
| 84 | - return $code; |
|
| 84 | + return $code; |
|
| 85 | + } |
|
| 85 | 86 | } |
| 86 | - } |
|
| 87 | 87 | |
| 88 | - public function getResult($key = null) |
|
| 89 | - { |
|
| 88 | + public function getResult($key = null) |
|
| 89 | + { |
|
| 90 | 90 | if($key != null) |
| 91 | 91 | { |
| 92 | - if(isset($this->responses[$key]['code'])) |
|
| 93 | - { |
|
| 92 | + if(isset($this->responses[$key]['code'])) |
|
| 93 | + { |
|
| 94 | 94 | return $this->responses[$key]; |
| 95 | - } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - $innerSleepInt = $outerSleepInt = 1; |
|
| 98 | - while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) |
|
| 99 | - { |
|
| 97 | + $innerSleepInt = $outerSleepInt = 1; |
|
| 98 | + while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) |
|
| 99 | + { |
|
| 100 | 100 | usleep(intval($outerSleepInt)); |
| 101 | 101 | $outerSleepInt = intval(max(1, ($outerSleepInt*$this->sleepIncrement))); |
| 102 | 102 | $ms=curl_multi_select($this->mc, 0); |
@@ -104,114 +104,114 @@ discard block |
||
| 104 | 104 | // bug in PHP 5.3.18+ where curl_multi_select can return -1 |
| 105 | 105 | // https://bugs.php.net/bug.php?id=63411 |
| 106 | 106 | if($ms === -1) |
| 107 | - usleep(100000); |
|
| 107 | + usleep(100000); |
|
| 108 | 108 | |
| 109 | 109 | // see pull request https://github.com/jmathai/php-multi-curl/pull/17 |
| 110 | 110 | // details here http://curl.haxx.se/libcurl/c/libcurl-errors.html |
| 111 | 111 | if($ms >= CURLM_CALL_MULTI_PERFORM) |
| 112 | 112 | { |
| 113 | - do{ |
|
| 113 | + do{ |
|
| 114 | 114 | $this->execStatus = curl_multi_exec($this->mc, $this->running); |
| 115 | 115 | usleep(intval($innerSleepInt)); |
| 116 | 116 | $innerSleepInt = intval(max(1, ($innerSleepInt*$this->sleepIncrement))); |
| 117 | - }while($this->execStatus==CURLM_CALL_MULTI_PERFORM); |
|
| 118 | - $innerSleepInt = 1; |
|
| 117 | + }while($this->execStatus==CURLM_CALL_MULTI_PERFORM); |
|
| 118 | + $innerSleepInt = 1; |
|
| 119 | 119 | } |
| 120 | 120 | $this->storeResponses(); |
| 121 | 121 | if(isset($this->responses[$key]['data'])) |
| 122 | 122 | { |
| 123 | - return $this->responses[$key]; |
|
| 123 | + return $this->responses[$key]; |
|
| 124 | 124 | } |
| 125 | - } |
|
| 126 | - return null; |
|
| 125 | + } |
|
| 126 | + return null; |
|
| 127 | 127 | } |
| 128 | 128 | return false; |
| 129 | - } |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - public static function getSequence() |
|
| 132 | - { |
|
| 131 | + public static function getSequence() |
|
| 132 | + { |
|
| 133 | 133 | return new MultiCurlSequence(self::$timers); |
| 134 | - } |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - public static function getTimers() |
|
| 137 | - { |
|
| 136 | + public static function getTimers() |
|
| 137 | + { |
|
| 138 | 138 | return self::$timers; |
| 139 | - } |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - public function inject($key, $value) |
|
| 142 | - { |
|
| 141 | + public function inject($key, $value) |
|
| 142 | + { |
|
| 143 | 143 | $this->$key = $value; |
| 144 | - } |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - private function getKey($ch) |
|
| 147 | - { |
|
| 146 | + private function getKey($ch) |
|
| 147 | + { |
|
| 148 | 148 | return (string)$ch; |
| 149 | - } |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - private function headerCallback($ch, $header) |
|
| 152 | - { |
|
| 151 | + private function headerCallback($ch, $header) |
|
| 152 | + { |
|
| 153 | 153 | $_header = trim($header); |
| 154 | 154 | $colonPos= strpos($_header, ':'); |
| 155 | 155 | if($colonPos > 0) |
| 156 | 156 | { |
| 157 | - $key = substr($_header, 0, $colonPos); |
|
| 158 | - $val = preg_replace('/^\W+/','',substr($_header, $colonPos)); |
|
| 159 | - $this->responses[$this->getKey($ch)]['headers'][$key] = $val; |
|
| 157 | + $key = substr($_header, 0, $colonPos); |
|
| 158 | + $val = preg_replace('/^\W+/','',substr($_header, $colonPos)); |
|
| 159 | + $this->responses[$this->getKey($ch)]['headers'][$key] = $val; |
|
| 160 | 160 | } |
| 161 | 161 | return strlen($header); |
| 162 | - } |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - private function storeResponses() |
|
| 165 | - { |
|
| 164 | + private function storeResponses() |
|
| 165 | + { |
|
| 166 | 166 | while($done = curl_multi_info_read($this->mc)) |
| 167 | 167 | { |
| 168 | - $this->storeResponse($done); |
|
| 168 | + $this->storeResponse($done); |
|
| 169 | + } |
|
| 169 | 170 | } |
| 170 | - } |
|
| 171 | 171 | |
| 172 | - private function storeResponse($done, $isAsynchronous = true) |
|
| 173 | - { |
|
| 172 | + private function storeResponse($done, $isAsynchronous = true) |
|
| 173 | + { |
|
| 174 | 174 | $key = $this->getKey($done['handle']); |
| 175 | 175 | $this->stopTimer($key, $done); |
| 176 | 176 | if($isAsynchronous) |
| 177 | - $this->responses[$key]['data'] = curl_multi_getcontent($done['handle']); |
|
| 177 | + $this->responses[$key]['data'] = curl_multi_getcontent($done['handle']); |
|
| 178 | 178 | else |
| 179 | - $this->responses[$key]['data'] = curl_exec($done['handle']); |
|
| 179 | + $this->responses[$key]['data'] = curl_exec($done['handle']); |
|
| 180 | 180 | |
| 181 | 181 | $this->responses[$key]['response'] = $this->responses[$key]['data']; |
| 182 | 182 | |
| 183 | 183 | foreach($this->properties as $name => $const) |
| 184 | 184 | { |
| 185 | - $this->responses[$key][$name] = curl_getinfo($done['handle'], $const); |
|
| 185 | + $this->responses[$key][$name] = curl_getinfo($done['handle'], $const); |
|
| 186 | 186 | } |
| 187 | 187 | if($isAsynchronous) |
| 188 | - curl_multi_remove_handle($this->mc, $done['handle']); |
|
| 188 | + curl_multi_remove_handle($this->mc, $done['handle']); |
|
| 189 | 189 | curl_close($done['handle']); |
| 190 | - } |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - private function startTimer($key) |
|
| 193 | - { |
|
| 192 | + private function startTimer($key) |
|
| 193 | + { |
|
| 194 | 194 | self::$timers[$key]['start'] = microtime(true); |
| 195 | - } |
|
| 196 | - |
|
| 197 | - private function stopTimer($key, $done) |
|
| 198 | - { |
|
| 199 | - self::$timers[$key]['end'] = microtime(true); |
|
| 200 | - self::$timers[$key]['api'] = curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL); |
|
| 201 | - self::$timers[$key]['time'] = curl_getinfo($done['handle'], CURLINFO_TOTAL_TIME); |
|
| 202 | - self::$timers[$key]['code'] = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - public static function getInstance() |
|
| 206 | - { |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + private function stopTimer($key, $done) |
|
| 198 | + { |
|
| 199 | + self::$timers[$key]['end'] = microtime(true); |
|
| 200 | + self::$timers[$key]['api'] = curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL); |
|
| 201 | + self::$timers[$key]['time'] = curl_getinfo($done['handle'], CURLINFO_TOTAL_TIME); |
|
| 202 | + self::$timers[$key]['code'] = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + public static function getInstance() |
|
| 206 | + { |
|
| 207 | 207 | if(self::$inst == null) |
| 208 | 208 | { |
| 209 | - self::$singleton = 1; |
|
| 210 | - self::$inst = new MultiCurl(); |
|
| 209 | + self::$singleton = 1; |
|
| 210 | + self::$inst = new MultiCurl(); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | return self::$inst; |
| 214 | - } |
|
| 214 | + } |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /* |
@@ -6,24 +6,24 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class MultiCurlManager |
| 8 | 8 | { |
| 9 | - private $key; |
|
| 10 | - private $epiCurl; |
|
| 9 | + private $key; |
|
| 10 | + private $epiCurl; |
|
| 11 | 11 | |
| 12 | - public function __construct($key) |
|
| 13 | - { |
|
| 12 | + public function __construct($key) |
|
| 13 | + { |
|
| 14 | 14 | $this->key = $key; |
| 15 | 15 | $this->epiCurl = MultiCurl::getInstance(); |
| 16 | - } |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - public function __get($name) |
|
| 19 | - { |
|
| 18 | + public function __get($name) |
|
| 19 | + { |
|
| 20 | 20 | $responses = $this->epiCurl->getResult($this->key); |
| 21 | 21 | return isset($responses[$name]) ? $responses[$name] : null; |
| 22 | - } |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function __isset($name) |
|
| 25 | - { |
|
| 24 | + public function __isset($name) |
|
| 25 | + { |
|
| 26 | 26 | $val = self::__get($name); |
| 27 | 27 | return empty($val); |
| 28 | - } |
|
| 28 | + } |
|
| 29 | 29 | } |