Skip to content

Commit

Permalink
Initial checkin of v2 compatible library.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmathai committed Jan 6, 2011
1 parent 07c0bb2 commit fd5b747
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 568 deletions.
38 changes: 32 additions & 6 deletions EpiCurl.php
Expand Up @@ -13,6 +13,7 @@ class EpiCurl
private $requests = array();
private $responses = array();
private $properties = array();
private static $timers = array();

function __construct()
{
Expand All @@ -38,6 +39,7 @@ public function addCurl($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)
Expand Down Expand Up @@ -67,14 +69,14 @@ public function getResult($key = null)
while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM))
{
usleep($outerSleepInt);
$outerSleepInt *= $this->sleepIncrement;
$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 *= $this->sleepIncrement;
$innerSleepInt = intval(max(1, ($innerSleepInt*$this->sleepIncrement)));
}while($this->execStatus==CURLM_CALL_MULTI_PERFORM);
$innerSleepInt = 1;
}
Expand All @@ -90,6 +92,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;
Expand All @@ -113,6 +125,7 @@ private function storeResponses()
while($done = curl_multi_info_read($this->mc))
{
$key = (string)$done['handle'];
$this->stopTimer($key, $done);
$this->responses[$key]['data'] = curl_multi_getcontent($done['handle']);
foreach($this->properties as $name => $const)
{
Expand All @@ -123,6 +136,19 @@ private function storeResponses()
}
}

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)
Expand All @@ -140,19 +166,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 $responses[$name];
return isset($responses[$name]) ? $responses[$name] : null;
}

function __isset($name)
public function __isset($name)
{
$val = self::__get($name);
return empty($val);
Expand Down
119 changes: 47 additions & 72 deletions EpiFoursquare.php
Expand Up @@ -9,50 +9,41 @@
*
* @author Jaisen Mathai <jaisen@jmathai.com>
*/
class EpiFoursquare extends EpiOAuth
class EpiFoursquare
{
const EPIFOURSQUARE_SIGNATURE_METHOD = 'HMAC-SHA1';
const EPIFOURSQUARE_AUTH_OAUTH = 'oauth';
const EPIFOURSQUARE_AUTH_BASIC = 'basic';
protected $requestTokenUrl= 'http://foursquare.com/oauth/request_token';
protected $accessTokenUrl = 'http://foursquare.com/oauth/access_token';
protected $authorizeUrl = 'http://foursquare.com/oauth/authorize';
//protected $authenticateUrl= 'http://foursquare.com/oauth/authorize'; // In case four square implements sign in with like Twitter
protected $apiUrl = 'http://api.foursquare.com';
protected $clientId, $clientSecret, $accessToken;
protected $requestTokenUrl= 'https://foursquare.com/oauth2/authenticate';
protected $accessTokenUrl = 'https://foursquare.com/oauth2/access_token';
protected $authorizeUrl = 'https://foursquare.com/oauth/authorize';
protected $apiUrl = 'https://api.foursquare.com';
protected $userAgent = 'EpiFoursquare (http://github.com/jmathai/foursquare-async/tree/)';
protected $apiVersion = 'v1';
protected $apiVersion = 'v2';
protected $isAsynchronous = false;
protected $followLocation = false;
protected $connectionTimeout = 5;
protected $requestTimeout = 30;
protected $debug = false;

/* OAuth methods */
public function delete($endpoint, $params = null)
{
return $this->request('DELETE', $endpoint, $params);
}

public function get($endpoint, $params = null)
{
return $this->request('GET', $endpoint, $params);
}

public function post($endpoint, $params = null)
public function getAccessToken($code, $redirectUri)
{
return $this->request('POST', $endpoint, $params);
$params = array('client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'grant_type' => 'authorization_code', 'redirect_uri' => $redirectUri, 'code' => $code);
$qs = http_build_query($params);
return $this->request('GET', "{$this->accessTokenUrl}?{$qs}");
}

/* Basic auth methods */
public function delete_basic($endpoint, $params = null, $username = null, $password = null)
public function getAuthorizeUrl($redirectUri)
{
return $this->request_basic('DELETE', $endpoint, $params, $username, $password);
$params = array('client_id' => $this->clientId, 'response_type' => 'code', 'redirect_uri' => $redirectUri);
$qs = http_build_query($params);
return "{$this->requestTokenUrl}?{$qs}";
}

public function get_basic($endpoint, $params = null, $username = null, $password = null)
public function setTimeout($requestTimeout = null, $connectionTimeout = null)
{
return $this->request_basic('GET', $endpoint, $params, $username, $password);
}

public function post_basic($endpoint, $params = null, $username = null, $password = null)
{
return $this->request_basic('POST', $endpoint, $params, $username, $password);
if($requestTimeout !== null)
$this->requestTimeout = floatval($requestTimeout);
if($connectionTimeout !== null)
$this->connectionTimeout = floatval($connectionTimeout);
}

public function useApiVersion($version = null)
Expand All @@ -65,35 +56,27 @@ public function useAsynchronous($async = true)
$this->isAsynchronous = (bool)$async;
}

public function __construct($consumerKey = null, $consumerSecret = null, $oauthToken = null, $oauthTokenSecret = null)
// Public api interface for most calls GET/POST/DELETE
public function delete($endpoint, $params = null)
{
parent::__construct($consumerKey, $consumerSecret, self::EPIFOURSQUARE_SIGNATURE_METHOD);
$this->setToken($oauthToken, $oauthTokenSecret);
return $this->request('DELETE', $endpoint, $params);
}

public function __call($name, $params = null/*, $username, $password*/)
public function get($endpoint, $params = null)
{
$parts = explode('_', $name);
$method = strtoupper(array_shift($parts));
$parts = implode('_', $parts);
$endpoint = '/' . preg_replace('/[A-Z]|[0-9]+/e', "'/'.strtolower('\\0')", $parts) . '.json';
/* HACK: this is required for list support that starts with a user id */
$endpoint = str_replace('//','/',$endpoint);
$args = !empty($params) ? array_shift($params) : null;

// calls which do not have a consumerKey are assumed to not require authentication
if($this->consumerKey === null)
{
if(!empty($params))
{
$username = array_shift($params);
$password = !empty($params) ? array_shift($params) : null;
}
return $this->request('GET', $endpoint, $params);
}

return $this->request_basic($method, $endpoint, $args, $username, $password);
}
public function post($endpoint, $params = null)
{
return $this->request('POST', $endpoint, $params);
}

return $this->request($method, $endpoint, $args);
public function __construct($clientId = null, $clientSecret = null, $accessToken = null)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->accessToken = $accessToken;
}

private function getApiUrl($endpoint)
Expand All @@ -104,19 +87,16 @@ private function getApiUrl($endpoint)
return "{$this->apiUrl}{$endpoint}";
}

private function request($method, $endpoint, $params = null)
private function request($method, $endpoint, $params = null, $username = null, $password = null)
{
$url = $this->getUrl($this->getApiUrl($endpoint));
$resp= new EpiFoursquareJson(call_user_func(array($this, 'httpRequest'), $method, $url, $params, $this->isMultipart($params)), $this->debug);
if(!$this->isAsynchronous)
$resp->responseText;
if(preg_match('#^https?://#', $endpoint))
$url = $endpoint;
else
$url = $this->getApiUrl($endpoint);

return $resp;
}
if($this->accessToken)
$params['oauth_token'] = $this->accessToken;

private function request_basic($method, $endpoint, $params = null, $username = null, $password = null)
{
$url = $this->getApiUrl($endpoint);
if($method === 'GET')
$url .= is_null($params) ? '' : '?'.http_build_query($params, '', '&');
$ch = curl_init($url);
Expand All @@ -126,13 +106,8 @@ private function request_basic($method, $endpoint, $params = null, $username = n
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($method === 'POST' && $params !== null)
{
if($this->isMultipart($params))
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
else
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->buildHttpQueryRaw($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
if(!empty($username) && !empty($password))
curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");

$resp = new EpiFoursquareJson(EpiCurl::getInstance()->addCurl($ch), $this->debug);
if(!$this->isAsynchronous)
Expand Down

0 comments on commit fd5b747

Please sign in to comment.