-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
2,924 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright (c) 2010, makoto_kw, http://www.makotokw.com | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
* The names of its contributors may NOT be used to endorse or promote | ||
products derived from this software without specific prior written | ||
permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY Jaisen Mathai "AS IS" AND ANY | ||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL Jaisen Mathai BE LIABLE FOR ANY | ||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
require_once dirname(__FILE__).'/../src/Twitter.php'; | ||
try { | ||
$twitter = new Twitter(); | ||
echo sprintf("Use %s class\r\n", $twitter->getRequestClass()); | ||
|
||
echo 'Account: '; $id = rtrim(fgets(STDIN,4096)); | ||
echo 'Password: '; $pw = rtrim(fgets(STDIN,4096)); | ||
$twitter->basicAuth($id, $pw); | ||
echo 'Input Status: '; $status = rtrim(fgets(STDIN,4096)); | ||
if (empty($status)) { | ||
exit; | ||
} | ||
$twitter->call('statuses/update',array('status'=>$status)); | ||
} catch (Exception $e) { | ||
echo $e; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
require_once dirname(__FILE__).'/../src/Twitter.php'; | ||
try { | ||
// php-twilent | ||
$consumer_key = 'Y8rq4tcFLhqVEUKV4FvZA'; | ||
$consumer_secret = 'l2PLP0BCr2pJ5amdSDIRDbtaBVku6QEf3iCNvo8CzeE'; | ||
|
||
$twitter = new Twitter(); | ||
echo sprintf("Use %s class\r\n", $twitter->getRequestClass()); | ||
$oauth = $twitter->oAuth($consumer_key, $consumer_secret); | ||
$requestToken = $oauth->getRequestToken(); | ||
$url = $oauth->getAuthorizeUrl($requestToken); | ||
echo 'Go to '.$url."\r\n"; | ||
echo 'Input PIN: '; | ||
$pin = trim(fgets(STDIN,4096)); | ||
if (empty($pin)) exit; | ||
$token = $oauth->getAccessToken($pin); | ||
echo 'Your token is "'.$token['oauth_token']."\"\r\n"; | ||
echo 'Your secret token is "'.$token['oauth_token_secret']."\"\r\n"; | ||
echo 'Input Status: '; | ||
$status = trim(fgets(STDIN,4096)); | ||
if (empty($status)) exit; | ||
$twitter->call('statuses/update',array('status'=>$status)); | ||
|
||
} catch (Exception $e) { | ||
echo $e; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
<?php | ||
/** | ||
* Twitter class | ||
* | ||
* PHP versions 5 | ||
* | ||
* @author makoto_kw <[email protected]> | ||
* @version 0.1 | ||
* @license New BSD License, http://www.opensource.org/licenses/bsd-license.php | ||
* @link http://github.com/makotokw/php-twient | ||
*/ | ||
class Twitter | ||
{ | ||
const NAME = 'php-twient'; | ||
const VERSION = '0.1'; | ||
const URL = 'http://twitter.com'; | ||
const API_URL = 'http://api.twitter.com'; | ||
|
||
static protected $_path = null; | ||
protected $_userAgent = null; | ||
protected $_request = null; | ||
protected $_auth = null; | ||
protected $_requestClass = 'Twitter_Request'; | ||
protected $_defaultConfigration = array( | ||
'url'=>'http://twitter.com', | ||
'required'=>array(), | ||
'#id'=>null, | ||
'method'=>'get', | ||
'auth'=>true, | ||
); | ||
// http://apiwiki.twitter.com/Twitter-API-Documentation | ||
protected $_apis = array( | ||
// Timeline Methods | ||
'statuses/public_timeline' => array('auth'=>false), | ||
'statuses/friends_timeline' => array(), | ||
'statuses/home_timeline' => array('url'=>'http://api.twitter.com/1'), | ||
'statuses/user_timeline' => array('#id'=>'id'), | ||
'statuses/mentions' => array(), | ||
'statuses/retweeted_by_me' => array('url'=>'http://api.twitter.com/1'), | ||
'statuses/retweeted_to_me' => array('url'=>'http://api.twitter.com/1'), | ||
'statuses/retweets_of_me' => array('url'=>'http://api.twitter.com/1'), | ||
|
||
// Status Methods | ||
'statuses/show' => array('required'=>array('id'),'#id'=>'id','auth'=>false), | ||
'statuses/update' => array('required'=>array('status'),'method'=>'post'), | ||
'statuses/destroy' => array('required'=>array('id'),'#id'=>'id','method'=>'post'), | ||
'statuses/retweet' => array('url'=>'http://api.twitter.com/1','required'=>array('id'),'#id'=>'id','method'=>'post'), | ||
'statuses/retweets' => array('url'=>'http://api.twitter.com/1','required'=>array('id'),'#id'=>'id'), | ||
); | ||
|
||
/** | ||
* Construct | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->_requestClass = (function_exists('curl_init')) ? 'Twitter_Request_Curl' : 'Twitter_Request'; | ||
} | ||
|
||
/** | ||
* Gets the path | ||
* | ||
* @return string | ||
*/ | ||
public static function getPath() | ||
{ | ||
if (!self::$_path) { | ||
self::$_path = dirname(__FILE__); | ||
} | ||
return self::$_path; | ||
} | ||
|
||
/** | ||
* Twitter autoload | ||
* spl_autoload_register(array('Twitter', 'autoload')); | ||
* @param string $classname | ||
* @return bool | ||
*/ | ||
public static function autoload($className) | ||
{ | ||
if (class_exists($className, false) || interface_exists($className, false)) { | ||
return false; | ||
} | ||
$class = self::getPath() . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; | ||
if (file_exists($class)) { | ||
require $class; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* create Twitter Request object | ||
* @param string $className Twitter_Request or Twitter_Request_Curl | ||
* @return Twitter_Request | ||
*/ | ||
public function createRequest($className = null) | ||
{ | ||
return ($className!=null) ? new $className : new $this->_requestClass; | ||
} | ||
|
||
/** | ||
* Sets a request class name | ||
* @param string $className Twitter_Request or Twitter_Request_Curl | ||
*/ | ||
public function setRequestClass($className) | ||
{ | ||
$this->_requestClass = (string)$className;; | ||
} | ||
|
||
/** | ||
* Gets the request class name | ||
* @return string | ||
*/ | ||
public function getRequestClass() | ||
{ | ||
return $this->_requestClass; | ||
} | ||
|
||
/** | ||
* Sets up a Basic Auth | ||
* @param string $username e-mail or account for twitter | ||
* @param string $password password for twitter | ||
* @return Twitter_Auth_Basic | ||
*/ | ||
public function basicAuth($username, $password) | ||
{ | ||
$this->_auth = new Twitter_Auth_Basic($username, $password); | ||
return $this->_auth; | ||
} | ||
|
||
/** | ||
* Sets up an OAuth | ||
* @param string $consumerKey | ||
* @param string $consumerSecret | ||
* @param string $oauthToken | ||
* @param string $oauthTokenSecret | ||
* @return Twitter_Auth_OAuth | ||
*/ | ||
public function oAuth($consumerKey, $consumerSecret, $oauthToken = null, $oauthTokenSecret = null) | ||
{ | ||
$this->_auth = new Twitter_Auth_OAuth($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret); | ||
return $this->_auth; | ||
} | ||
|
||
/** | ||
* Sets an auth | ||
* @param Twitter_Auth $auth | ||
*/ | ||
public function setAuth($auth) | ||
{ | ||
$this->_auth = $auth; | ||
} | ||
|
||
/** | ||
* Gets an auth | ||
* @return Twitter_Auth | ||
*/ | ||
public function getAuth() | ||
{ | ||
return $this->_auth; | ||
} | ||
|
||
/** | ||
* Calls the REST API Method | ||
* @param string $methodName method name (ie. statuses/public_timeline) | ||
* @param array $params key-value parameter for method | ||
* @return mixed | ||
*/ | ||
public function call($methodName, array $params = array()) | ||
{ | ||
if (!array_key_exists($methodName, $this->_apis)) { | ||
throw new Twitter_Exception('This method is not supported: '.$methodName); | ||
} | ||
$config = array_merge($this->_defaultConfigration, $this->_apis[$methodName]); | ||
|
||
foreach ($config['required'] as $key) { | ||
if (!array_key_exists($key, $params)) { | ||
throw new Twitter_Exception($key.' is required'); | ||
} | ||
} | ||
|
||
// make url {method}.{format} or {method}/{id}.{format} | ||
$format = 'json'; | ||
$key = $config['#id']; | ||
$id = ($key!=null && array_key_exists($key,$params)) ? $params[$key] : null; | ||
if ($id!=null) { | ||
unset($params[$key]); | ||
$url = sprintf('%s/%s/%s.%s',$config['url'], $methodName, $id, $format); | ||
} else { | ||
$url = sprintf('%s/%s.%s',$config['url'], $methodName, $format); | ||
} | ||
|
||
$request = $this->createRequest(); | ||
$request->setUserAgent($this->getUserAgent()); | ||
$method = $config['method'].'JSON'; | ||
return $request->$method($url, $params, ($config['auth']) ? $this->getAuth() : null); | ||
} | ||
|
||
/** | ||
* Gets a user agent for the HTTP Request to twitter | ||
* @return string user agent | ||
*/ | ||
public function getUserAgent() | ||
{ | ||
return (!empty($this->_userAgent)) ? $this->_userAgent : self::NAME.'/'.self::VERSION; | ||
} | ||
|
||
/** | ||
* Sets a user agent for the HTTP Request to twitter | ||
* @param string $userAgent user agent | ||
*/ | ||
public function setUserAgent($userAgent) | ||
{ | ||
$this->_userAgent = (string)$userAgent; | ||
} | ||
} | ||
|
||
// include or autoload | ||
if (function_exists('spl_autoload_register')) { | ||
spl_autoload_register(array('Twitter', 'autoload')); | ||
} else { | ||
require_once dirname(__FILE__).'/Twitter/Exception.php'; | ||
require_once dirname(__FILE__).'/Twitter/Auth.php'; | ||
require_once dirname(__FILE__).'/Twitter/Auth/Basic.php'; | ||
require_once dirname(__FILE__).'/Twitter/Auth/OAuth.php'; | ||
require_once dirname(__FILE__).'/Twitter/Request.php'; | ||
require_once dirname(__FILE__).'/Twitter/Request/Curl.php'; | ||
require_once dirname(__FILE__).'/Twitter/TinyUrl.php'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* Twitter_Auth class | ||
* | ||
* PHP versions 5 | ||
* | ||
* @author makoto_kw <[email protected]> | ||
* @license New BSD License, http://www.opensource.org/licenses/bsd-license.php | ||
*/ | ||
class Twitter_Auth | ||
{ | ||
public function sign(array $data){} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Twitter_Auth_Basic class | ||
* | ||
* PHP versions 5 | ||
* | ||
* @author makoto_kw <[email protected]> | ||
* @license New BSD License, http://www.opensource.org/licenses/bsd-license.php | ||
*/ | ||
class Twitter_Auth_Basic extends Twitter_Auth | ||
{ | ||
protected $_username; | ||
protected $_password; | ||
|
||
public function __construct($username = null, $password = null) | ||
{ | ||
$this->_username = $username; | ||
$this->_password = $password; | ||
} | ||
|
||
public function sign(array $data) | ||
{ | ||
$signedData = $data; | ||
$signedData['headers'] = array('Authorization: Basic '.base64_encode($this->_username.':'.$this->_password)); | ||
$method = strtolower($data['method']); | ||
switch ($method) { | ||
case 'get': | ||
$signedData['url'] .= '?'.http_build_query($data['params']); | ||
break; | ||
default: | ||
$signedData['post_data'] = http_build_query($data['params']); | ||
break; | ||
} | ||
return $signedData; | ||
} | ||
} |
Oops, something went wrong.