diff --git a/php/EpiOAuth.php b/php/EpiOAuth.php index 0d2771c..86fd329 100644 --- a/php/EpiOAuth.php +++ b/php/EpiOAuth.php @@ -5,6 +5,7 @@ class EpiOAuth protected $requestTokenUrl; protected $accessTokenUrl; + protected $authenticateUrl; protected $authorizeUrl; protected $consumerKey; protected $consumerSecret; @@ -18,10 +19,14 @@ public function getAccessToken() return new EpiOAuthResponse($resp); } - public function getAuthorizationUrl() + public function getAuthenticateUrl() { - $retval = "{$this->authorizeUrl}?"; + $token = $this->getRequestToken(); + return $this->authenticateUrl . '?oauth_token=' . $token->oauth_token; + } + public function getAuthorizationUrl() + { $token = $this->getRequestToken(); return $this->authorizeUrl . '?oauth_token=' . $token->oauth_token; } @@ -219,8 +224,8 @@ public function __construct($resp) public function __get($name) { - if($this->__resp->code < 200 || $this->__resp->code > 299) - return false; + if($this->__resp->code != 200) + EpiOAuthException::raise($this->__resp->data, $this->__resp->code); parse_str($this->__resp->data, $result); foreach($result as $k => $v) @@ -231,3 +236,23 @@ public function __get($name) return $result[$name]; } } + +class EpiOAuthException extends Exception +{ + public static function raise($message, $code) + { + switch($code) + { + case 400: + throw new EpiOAuthBadRequestException($message, $code); + case 401: + throw new EpiOAuthUnauthorizedException($message, $code); + default: + throw new EpiOAuthException($message, $code); + } + } +} + + +class EpiOAuthBadRequestException extends EpiOAuthException{} +class EpiOAuthUnauthorizedException extends EpiOAuthException{} diff --git a/php/EpiTwitter.php b/php/EpiTwitter.php index b0dac9f..ef7669f 100644 --- a/php/EpiTwitter.php +++ b/php/EpiTwitter.php @@ -15,6 +15,7 @@ class EpiTwitter extends EpiOAuth protected $requestTokenUrl= 'http://twitter.com/oauth/request_token'; protected $accessTokenUrl = 'http://twitter.com/oauth/access_token'; protected $authorizeUrl = 'http://twitter.com/oauth/authorize'; + protected $authenticateUrl= 'http://twitter.com/oauth/authenticate'; protected $apiUrl = 'http://twitter.com'; protected $searchUrl = 'http://search.twitter.com'; @@ -59,13 +60,13 @@ public function __construct($response) // Implementation of the IteratorAggregate::getIterator() to support foreach ($this as $...) public function getIterator () { - return new ArrayIterator($this->response); + return new ArrayIterator($this->__obj); } // Implementation of Countable::count() to support count($this) public function count () { - return count($this->response); + return count($this->__obj); } // Next four functions are to support ArrayAccess interface @@ -95,10 +96,13 @@ public function offsetGet($offset) public function __get($name) { + if($this->__resp->code != 200) + EpiOAuthException::raise($this->__resp->data, $this->__resp->code); + $this->responseText = $this->__resp->data; $this->response = json_decode($this->responseText, 1); - $obj = json_decode($this->responseText); - foreach($obj as $k => $v) + $this->__obj = json_decode($this->responseText); + foreach($this->__obj as $k => $v) { $this->$k = $v; } diff --git a/php/tests/EpiTwitterTest.php b/php/tests/EpiTwitterTest.php index 24aeb61..d739c00 100644 --- a/php/tests/EpiTwitterTest.php +++ b/php/tests/EpiTwitterTest.php @@ -67,9 +67,12 @@ function testDirectMessage() $this->assertTrue(!empty($resp->response['id']), "response id is empty"); } + /** + * @expectedException EpiOAuthException + */ function testNoRequiredParameter() { - $resp = $this->twitterObj->post_direct_messagesNew( array ( 'user' => $this->screenName, 'text' => "")); + $resp = $this->twitterObj->post_direct_messagesNew( array ( 'user' => $this->screenName, 'text' => '')); $this->assertTrue(!empty($resp->response['error']), "An empty direct message should return an error message"); }