@@ -13,45 +13,45 @@ 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 | - } |
|
| 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 | 46 | |
| 47 | - public function reset(){ |
|
| 48 | - $this->requests = array(); |
|
| 49 | - $this->responses = array(); |
|
| 50 | - self::$timers = array(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function addUrl($url, $options = array()) |
|
| 54 | - { |
|
| 47 | + public function reset(){ |
|
| 48 | + $this->requests = array(); |
|
| 49 | + $this->responses = array(); |
|
| 50 | + self::$timers = array(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function addUrl($url, $options = array()) |
|
| 54 | + { |
|
| 55 | 55 | $ch = curl_init($url); |
| 56 | 56 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 57 | 57 | foreach($options as $option=>$value) |
@@ -59,13 +59,13 @@ discard block |
||
| 59 | 59 | curl_setopt($ch, $option, $value); |
| 60 | 60 | } |
| 61 | 61 | return $this->addCurl($ch); |
| 62 | - } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - public function addCurl($ch) |
|
| 65 | - { |
|
| 64 | + public function addCurl($ch) |
|
| 65 | + { |
|
| 66 | 66 | if(gettype($ch) !== 'resource') |
| 67 | 67 | { |
| 68 | - throw new MultiCurlInvalidParameterException('Parameter must be a valid curl handle'); |
|
| 68 | + throw new MultiCurlInvalidParameterException('Parameter must be a valid curl handle'); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | $key = $this->getKey($ch); |
@@ -78,31 +78,31 @@ discard block |
||
| 78 | 78 | // (1) |
| 79 | 79 | if($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM) |
| 80 | 80 | { |
| 81 | - do |
|
| 82 | - { |
|
| 81 | + do |
|
| 82 | + { |
|
| 83 | 83 | $this->execStatus = curl_multi_exec($this->mc, $this->running); |
| 84 | - } while ($this->execStatus === CURLM_CALL_MULTI_PERFORM); |
|
| 84 | + } while ($this->execStatus === CURLM_CALL_MULTI_PERFORM); |
|
| 85 | 85 | |
| 86 | - return new MultiCurlManager($key); |
|
| 86 | + return new MultiCurlManager($key); |
|
| 87 | 87 | } |
| 88 | 88 | else |
| 89 | 89 | { |
| 90 | - return $code; |
|
| 90 | + return $code; |
|
| 91 | + } |
|
| 91 | 92 | } |
| 92 | - } |
|
| 93 | 93 | |
| 94 | - public function getResult($key = null) |
|
| 95 | - { |
|
| 94 | + public function getResult($key = null) |
|
| 95 | + { |
|
| 96 | 96 | if($key != null) |
| 97 | 97 | { |
| 98 | - if(isset($this->responses[$key]['code'])) |
|
| 99 | - { |
|
| 98 | + if(isset($this->responses[$key]['code'])) |
|
| 99 | + { |
|
| 100 | 100 | return $this->responses[$key]; |
| 101 | - } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - $innerSleepInt = $outerSleepInt = 1; |
|
| 104 | - while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) |
|
| 105 | - { |
|
| 103 | + $innerSleepInt = $outerSleepInt = 1; |
|
| 104 | + while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) |
|
| 105 | + { |
|
| 106 | 106 | usleep(intval($outerSleepInt)); |
| 107 | 107 | $outerSleepInt = intval(max(1, ($outerSleepInt*$this->sleepIncrement))); |
| 108 | 108 | $ms=curl_multi_select($this->mc, 0); |
@@ -110,114 +110,114 @@ discard block |
||
| 110 | 110 | // bug in PHP 5.3.18+ where curl_multi_select can return -1 |
| 111 | 111 | // https://bugs.php.net/bug.php?id=63411 |
| 112 | 112 | if($ms === -1) |
| 113 | - usleep(100000); |
|
| 113 | + usleep(100000); |
|
| 114 | 114 | |
| 115 | 115 | // see pull request https://github.com/jmathai/php-multi-curl/pull/17 |
| 116 | 116 | // details here http://curl.haxx.se/libcurl/c/libcurl-errors.html |
| 117 | 117 | if($ms >= CURLM_CALL_MULTI_PERFORM) |
| 118 | 118 | { |
| 119 | - do{ |
|
| 119 | + do{ |
|
| 120 | 120 | $this->execStatus = curl_multi_exec($this->mc, $this->running); |
| 121 | 121 | usleep(intval($innerSleepInt)); |
| 122 | 122 | $innerSleepInt = intval(max(1, ($innerSleepInt*$this->sleepIncrement))); |
| 123 | - }while($this->execStatus==CURLM_CALL_MULTI_PERFORM); |
|
| 124 | - $innerSleepInt = 1; |
|
| 123 | + }while($this->execStatus==CURLM_CALL_MULTI_PERFORM); |
|
| 124 | + $innerSleepInt = 1; |
|
| 125 | 125 | } |
| 126 | 126 | $this->storeResponses(); |
| 127 | 127 | if(isset($this->responses[$key]['data'])) |
| 128 | 128 | { |
| 129 | - return $this->responses[$key]; |
|
| 129 | + return $this->responses[$key]; |
|
| 130 | 130 | } |
| 131 | - } |
|
| 132 | - return null; |
|
| 131 | + } |
|
| 132 | + return null; |
|
| 133 | 133 | } |
| 134 | 134 | return false; |
| 135 | - } |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - public static function getSequence() |
|
| 138 | - { |
|
| 137 | + public static function getSequence() |
|
| 138 | + { |
|
| 139 | 139 | return new MultiCurlSequence(self::$timers); |
| 140 | - } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - public static function getTimers() |
|
| 143 | - { |
|
| 142 | + public static function getTimers() |
|
| 143 | + { |
|
| 144 | 144 | return self::$timers; |
| 145 | - } |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - public function inject($key, $value) |
|
| 148 | - { |
|
| 147 | + public function inject($key, $value) |
|
| 148 | + { |
|
| 149 | 149 | $this->$key = $value; |
| 150 | - } |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - private function getKey($ch) |
|
| 153 | - { |
|
| 152 | + private function getKey($ch) |
|
| 153 | + { |
|
| 154 | 154 | return (string)$ch; |
| 155 | - } |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - private function headerCallback($ch, $header) |
|
| 158 | - { |
|
| 157 | + private function headerCallback($ch, $header) |
|
| 158 | + { |
|
| 159 | 159 | $_header = trim($header); |
| 160 | 160 | $colonPos= strpos($_header, ':'); |
| 161 | 161 | if($colonPos > 0) |
| 162 | 162 | { |
| 163 | - $key = substr($_header, 0, $colonPos); |
|
| 164 | - $val = preg_replace('/^\W+/','',substr($_header, $colonPos)); |
|
| 165 | - $this->responses[$this->getKey($ch)]['headers'][$key] = $val; |
|
| 163 | + $key = substr($_header, 0, $colonPos); |
|
| 164 | + $val = preg_replace('/^\W+/','',substr($_header, $colonPos)); |
|
| 165 | + $this->responses[$this->getKey($ch)]['headers'][$key] = $val; |
|
| 166 | 166 | } |
| 167 | 167 | return strlen($header); |
| 168 | - } |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - private function storeResponses() |
|
| 171 | - { |
|
| 170 | + private function storeResponses() |
|
| 171 | + { |
|
| 172 | 172 | while($done = curl_multi_info_read($this->mc)) |
| 173 | 173 | { |
| 174 | - $this->storeResponse($done); |
|
| 174 | + $this->storeResponse($done); |
|
| 175 | + } |
|
| 175 | 176 | } |
| 176 | - } |
|
| 177 | 177 | |
| 178 | - private function storeResponse($done, $isAsynchronous = true) |
|
| 179 | - { |
|
| 178 | + private function storeResponse($done, $isAsynchronous = true) |
|
| 179 | + { |
|
| 180 | 180 | $key = $this->getKey($done['handle']); |
| 181 | 181 | $this->stopTimer($key, $done); |
| 182 | 182 | if($isAsynchronous) |
| 183 | - $this->responses[$key]['data'] = curl_multi_getcontent($done['handle']); |
|
| 183 | + $this->responses[$key]['data'] = curl_multi_getcontent($done['handle']); |
|
| 184 | 184 | else |
| 185 | - $this->responses[$key]['data'] = curl_exec($done['handle']); |
|
| 185 | + $this->responses[$key]['data'] = curl_exec($done['handle']); |
|
| 186 | 186 | |
| 187 | 187 | $this->responses[$key]['response'] = $this->responses[$key]['data']; |
| 188 | 188 | |
| 189 | 189 | foreach($this->properties as $name => $const) |
| 190 | 190 | { |
| 191 | - $this->responses[$key][$name] = curl_getinfo($done['handle'], $const); |
|
| 191 | + $this->responses[$key][$name] = curl_getinfo($done['handle'], $const); |
|
| 192 | 192 | } |
| 193 | 193 | if($isAsynchronous) |
| 194 | - curl_multi_remove_handle($this->mc, $done['handle']); |
|
| 194 | + curl_multi_remove_handle($this->mc, $done['handle']); |
|
| 195 | 195 | curl_close($done['handle']); |
| 196 | - } |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - private function startTimer($key) |
|
| 199 | - { |
|
| 198 | + private function startTimer($key) |
|
| 199 | + { |
|
| 200 | 200 | self::$timers[$key]['start'] = microtime(true); |
| 201 | - } |
|
| 202 | - |
|
| 203 | - private function stopTimer($key, $done) |
|
| 204 | - { |
|
| 205 | - self::$timers[$key]['end'] = microtime(true); |
|
| 206 | - self::$timers[$key]['api'] = curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL); |
|
| 207 | - self::$timers[$key]['time'] = curl_getinfo($done['handle'], CURLINFO_TOTAL_TIME); |
|
| 208 | - self::$timers[$key]['code'] = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - public static function getInstance() |
|
| 212 | - { |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + private function stopTimer($key, $done) |
|
| 204 | + { |
|
| 205 | + self::$timers[$key]['end'] = microtime(true); |
|
| 206 | + self::$timers[$key]['api'] = curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL); |
|
| 207 | + self::$timers[$key]['time'] = curl_getinfo($done['handle'], CURLINFO_TOTAL_TIME); |
|
| 208 | + self::$timers[$key]['code'] = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + public static function getInstance() |
|
| 212 | + { |
|
| 213 | 213 | if(self::$inst == null) |
| 214 | 214 | { |
| 215 | - self::$singleton = 1; |
|
| 216 | - self::$inst = new MultiCurl(); |
|
| 215 | + self::$singleton = 1; |
|
| 216 | + self::$inst = new MultiCurl(); |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | return self::$inst; |
| 220 | - } |
|
| 220 | + } |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | /* |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | public function __construct() |
| 31 | 31 | { |
| 32 | - if(self::$singleton === 0) |
|
| 32 | + if (self::$singleton === 0) |
|
| 33 | 33 | { |
| 34 | 34 | throw new MultiCurlException('This class cannot be instantiated by the new keyword. You must instantiate it using: $obj = MultiCurl::getInstance();'); |
| 35 | 35 | } |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | ); |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - public function reset(){ |
|
| 47 | + public function reset() { |
|
| 48 | 48 | $this->requests = array(); |
| 49 | 49 | $this->responses = array(); |
| 50 | 50 | self::$timers = array(); |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | { |
| 55 | 55 | $ch = curl_init($url); |
| 56 | 56 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 57 | - foreach($options as $option=>$value) |
|
| 57 | + foreach ($options as $option=>$value) |
|
| 58 | 58 | { |
| 59 | 59 | curl_setopt($ch, $option, $value); |
| 60 | 60 | } |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | |
| 64 | 64 | public function addCurl($ch) |
| 65 | 65 | { |
| 66 | - if(gettype($ch) !== 'resource') |
|
| 66 | + if (gettype($ch) !== 'resource') |
|
| 67 | 67 | { |
| 68 | 68 | throw new MultiCurlInvalidParameterException('Parameter must be a valid curl handle'); |
| 69 | 69 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | $this->startTimer($key); |
| 77 | 77 | |
| 78 | 78 | // (1) |
| 79 | - if($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM) |
|
| 79 | + if ($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM) |
|
| 80 | 80 | { |
| 81 | 81 | do |
| 82 | 82 | { |
@@ -93,38 +93,38 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | public function getResult($key = null) |
| 95 | 95 | { |
| 96 | - if($key != null) |
|
| 96 | + if ($key != null) |
|
| 97 | 97 | { |
| 98 | - if(isset($this->responses[$key]['code'])) |
|
| 98 | + if (isset($this->responses[$key]['code'])) |
|
| 99 | 99 | { |
| 100 | 100 | return $this->responses[$key]; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | $innerSleepInt = $outerSleepInt = 1; |
| 104 | - while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) |
|
| 104 | + while ($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) |
|
| 105 | 105 | { |
| 106 | 106 | usleep(intval($outerSleepInt)); |
| 107 | - $outerSleepInt = intval(max(1, ($outerSleepInt*$this->sleepIncrement))); |
|
| 108 | - $ms=curl_multi_select($this->mc, 0); |
|
| 107 | + $outerSleepInt = intval(max(1, ($outerSleepInt * $this->sleepIncrement))); |
|
| 108 | + $ms = curl_multi_select($this->mc, 0); |
|
| 109 | 109 | |
| 110 | 110 | // bug in PHP 5.3.18+ where curl_multi_select can return -1 |
| 111 | 111 | // https://bugs.php.net/bug.php?id=63411 |
| 112 | - if($ms === -1) |
|
| 112 | + if ($ms === -1) |
|
| 113 | 113 | usleep(100000); |
| 114 | 114 | |
| 115 | 115 | // see pull request https://github.com/jmathai/php-multi-curl/pull/17 |
| 116 | 116 | // details here http://curl.haxx.se/libcurl/c/libcurl-errors.html |
| 117 | - if($ms >= CURLM_CALL_MULTI_PERFORM) |
|
| 117 | + if ($ms >= CURLM_CALL_MULTI_PERFORM) |
|
| 118 | 118 | { |
| 119 | - do{ |
|
| 119 | + do { |
|
| 120 | 120 | $this->execStatus = curl_multi_exec($this->mc, $this->running); |
| 121 | 121 | usleep(intval($innerSleepInt)); |
| 122 | - $innerSleepInt = intval(max(1, ($innerSleepInt*$this->sleepIncrement))); |
|
| 123 | - }while($this->execStatus==CURLM_CALL_MULTI_PERFORM); |
|
| 122 | + $innerSleepInt = intval(max(1, ($innerSleepInt * $this->sleepIncrement))); |
|
| 123 | + } while ($this->execStatus == CURLM_CALL_MULTI_PERFORM); |
|
| 124 | 124 | $innerSleepInt = 1; |
| 125 | 125 | } |
| 126 | 126 | $this->storeResponses(); |
| 127 | - if(isset($this->responses[$key]['data'])) |
|
| 127 | + if (isset($this->responses[$key]['data'])) |
|
| 128 | 128 | { |
| 129 | 129 | return $this->responses[$key]; |
| 130 | 130 | } |
@@ -157,11 +157,11 @@ discard block |
||
| 157 | 157 | private function headerCallback($ch, $header) |
| 158 | 158 | { |
| 159 | 159 | $_header = trim($header); |
| 160 | - $colonPos= strpos($_header, ':'); |
|
| 161 | - if($colonPos > 0) |
|
| 160 | + $colonPos = strpos($_header, ':'); |
|
| 161 | + if ($colonPos > 0) |
|
| 162 | 162 | { |
| 163 | 163 | $key = substr($_header, 0, $colonPos); |
| 164 | - $val = preg_replace('/^\W+/','',substr($_header, $colonPos)); |
|
| 164 | + $val = preg_replace('/^\W+/', '', substr($_header, $colonPos)); |
|
| 165 | 165 | $this->responses[$this->getKey($ch)]['headers'][$key] = $val; |
| 166 | 166 | } |
| 167 | 167 | return strlen($header); |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | |
| 170 | 170 | private function storeResponses() |
| 171 | 171 | { |
| 172 | - while($done = curl_multi_info_read($this->mc)) |
|
| 172 | + while ($done = curl_multi_info_read($this->mc)) |
|
| 173 | 173 | { |
| 174 | 174 | $this->storeResponse($done); |
| 175 | 175 | } |
@@ -179,18 +179,18 @@ discard block |
||
| 179 | 179 | { |
| 180 | 180 | $key = $this->getKey($done['handle']); |
| 181 | 181 | $this->stopTimer($key, $done); |
| 182 | - if($isAsynchronous) |
|
| 182 | + if ($isAsynchronous) |
|
| 183 | 183 | $this->responses[$key]['data'] = curl_multi_getcontent($done['handle']); |
| 184 | 184 | else |
| 185 | 185 | $this->responses[$key]['data'] = curl_exec($done['handle']); |
| 186 | 186 | |
| 187 | 187 | $this->responses[$key]['response'] = $this->responses[$key]['data']; |
| 188 | 188 | |
| 189 | - foreach($this->properties as $name => $const) |
|
| 189 | + foreach ($this->properties as $name => $const) |
|
| 190 | 190 | { |
| 191 | 191 | $this->responses[$key][$name] = curl_getinfo($done['handle'], $const); |
| 192 | 192 | } |
| 193 | - if($isAsynchronous) |
|
| 193 | + if ($isAsynchronous) |
|
| 194 | 194 | curl_multi_remove_handle($this->mc, $done['handle']); |
| 195 | 195 | curl_close($done['handle']); |
| 196 | 196 | } |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | |
| 211 | 211 | public static function getInstance() |
| 212 | 212 | { |
| 213 | - if(self::$inst == null) |
|
| 213 | + if (self::$inst == null) |
|
| 214 | 214 | { |
| 215 | 215 | self::$singleton = 1; |
| 216 | 216 | self::$inst = new MultiCurl(); |