Permalink
Browse files

Merge remote branch 'upstream/master'

  • Loading branch information...
2 parents 483fe6c + 12fa620 commit 9b72019a560eaa558b9f433230c4b308a650f558 Nick Pope committed Oct 26, 2010
Showing with 178 additions and 58 deletions.
  1. +61 −14 EpiCurl.php
  2. +12 −3 EpiOAuth.php
  3. +54 −0 EpiSequence.php
  4. +0 −1 EpiTwitter.php
  5. +3 −3 README
  6. +21 −37 tests/EpiTwitterTest.php
  7. +27 −0 tests/sequencerTest.php
View
@@ -13,6 +13,7 @@ class EpiCurl
private $requests = array();
private $responses = array();
private $properties = array();
+ private static $timers = array();
function __construct()
{
@@ -31,13 +32,25 @@ function __construct()
);
}
+ 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)
@@ -66,15 +79,15 @@ public function getResult($key = null)
$innerSleepInt = $outerSleepInt = 1;
while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM))
{
- usleep($outerSleepInt);
- $outerSleepInt = max(1, ($outerSleepInt*$this->sleepIncrement));
+ 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($innerSleepInt);
- $innerSleepInt = max(1, ($innerSleepInt*$this->sleepIncrement));
+ usleep(intval($innerSleepInt));
+ $innerSleepInt = intval(max(1, ($innerSleepInt*$this->sleepIncrement)));
}while($this->execStatus==CURLM_CALL_MULTI_PERFORM);
$innerSleepInt = 1;
}
@@ -90,6 +103,16 @@ public function getResult($key = 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;
@@ -112,15 +135,39 @@ private function storeResponses()
{
while($done = curl_multi_info_read($this->mc))
{
- $key = (string)$done['handle'];
+ $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']);
- foreach($this->properties as $name => $const)
- {
- $this->responses[$key][$name] = curl_getinfo($done['handle'], $const);
- }
- curl_multi_remove_handle($this->mc, $done['handle']);
- curl_close($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()
@@ -140,19 +187,19 @@ class EpiCurlManager
private $key;
private $epiCurl;
- function __construct($key)
+ public function __construct($key)
{
$this->key = $key;
$this->epiCurl = EpiCurl::getInstance();
}
- function __get($name)
+ public function __get($name)
{
$responses = $this->epiCurl->getResult($this->key);
return isset($responses[$name]) ? $responses[$name] : null;
}
- function __isset($name)
+ public function __isset($name)
{
$val = self::__get($name);
return empty($val);
View
@@ -160,6 +160,7 @@ protected function curlInit($url)
curl_setopt($ch, CURLOPT_TIMEOUT, $this->requestTimeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+ curl_setopt($ch, CURLOPT_ENCODING, '');
if(isset($_SERVER ['SERVER_ADDR']) && !empty($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR'] != '127.0.0.1')
curl_setopt($ch, CURLOPT_INTERFACE, $_SERVER ['SERVER_ADDR']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
@@ -203,12 +204,20 @@ protected function generateSignature($method = null, $url = null, $params = null
return $this->signString($signatureBaseString);
}
+ protected function executeCurl($ch)
+ {
+ if($this->isAsynchronous)
+ return $this->curl->addCurl($ch);
+ else
+ return $this->curl->addEasyCurl($ch);
+ }
+
protected function httpDelete($url, $params) {
$this->addDefaultHeaders($url, $params['oauth']);
$ch = $this->curlInit($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->buildHttpQueryRaw($params['request']));
- $resp = $this->curl->addCurl($ch);
+ $resp = $this->executeCurl($ch);
$this->emptyHeaders();
return $resp;
}
@@ -226,7 +235,7 @@ protected function httpGet($url, $params = null)
}
$this->addDefaultHeaders($url, $params['oauth']);
$ch = $this->curlInit($url);
- $resp = $this->curl->addCurl($ch);
+ $resp = $this->executeCurl($ch);
$this->emptyHeaders();
return $resp;
@@ -243,7 +252,7 @@ protected function httpPost($url, $params = null, $isMultipart)
curl_setopt($ch, CURLOPT_POSTFIELDS, $params['request']);
else
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->buildHttpQueryRaw($params['request']));
- $resp = $this->curl->addCurl($ch);
+ $resp = $this->executeCurl($ch);
$this->emptyHeaders();
return $resp;
View
@@ -0,0 +1,54 @@
+<?php
+class EpiSequence
+{
+ private $timers, $min, $max, $width = 100;
+ public function __construct($timers)
+ {
+ $this->timers = $timers;
+
+ $min = PHP_INT_MAX;
+ $max = 0;
+ foreach($this->timers as $timer)
+ {
+ $min = min($timer['start'], $min);
+ $max = max($timer['end'], $max);
+ }
+ $this->min = $min;
+ $this->max = $max;
+ $this->range = $max-$min;
+ $this->step = floatval($this->range/$this->width);
+ }
+
+ public function renderAscii()
+ {
+ $tpl = '';
+ foreach($this->timers as $timer)
+ $tpl .= $this->tplAscii($timer);
+
+ return $tpl;
+ }
+
+ private function tplAscii($timer)
+ {
+ $lpad = $rpad = 0;
+ $lspace = $chars = $rspace = '';
+ if($timer['start'] > $this->min)
+ $lpad = intval(($timer['start'] - $this->min) / $this->step);
+ if($timer['end'] < $this->max)
+ $rpad = intval(($this->max - $timer['end']) / $this->step);
+ $mpad = $this->width - $lpad - $rpad;
+ if($lpad > 0)
+ $lspace = str_repeat(' ', $lpad);
+ if($mpad > 0)
+ $chars = str_repeat('=', $mpad);
+ if($rpad > 0)
+ $rspace = str_repeat(' ', $rpad);
+
+ $tpl = <<<TPL
+({$timer['api']} :: code={$timer['code']}, start={$timer['start']}, end={$timer['end']}, total={$timer['time']})
+[{$lspace}{$chars}{$rspace}]
+
+TPL;
+ return $tpl;
+ }
+}
View
@@ -247,7 +247,6 @@ class EpiTwitterException extends Exception
public static function raise($response, $debug)
{
$message = $response->data;
-
switch($response->code)
{
case 400:
View
6 README
@@ -8,11 +8,11 @@ Authors
* dingram
Documentation:
- * http://wiki.github.com/jmathai/twitter-async
+ * http://www.jaisenmathai.com/articles/twitter-async-documentation.html
Blogs
- * http://www.jaisenmathai.com/blog/2009/04/30/letting-your-users-sign-in-with-twitter-with-oauth/
- * http://www.jaisenmathai.com/blog/2009/03/31/how-to-quickly-integrate-with-twitters-oauth-api-using-php/
+ * http://www.jaisenmathai.com/articles/twitter-php-sign-in.html
+ * http://www.jaisenmathai.com/articles/twitter-php-oauth.html
* http://www.1stwebdesigner.com/tutorials/twitter-app-oauth-php/
Live Examples
Oops, something went wrong.

0 comments on commit 9b72019

Please sign in to comment.