This is a simple, easy to use Twitter API Package.
- Version: 1.0
- License: MIT License
- Author: Tevfik TÜMER
- Author of tmhOAuth: Matt Harris
You can simply download the package and extract it into fuel/packages/twitter
folder or you can go under your fuel/packages
folder and run this command:
$ git clone https://github.com/tevfik6/fuel-twitter twitter
First, If you don't have any twitter application, you will need to register an application from Twitter Developer.
Be careful! After you create your application don't forget to set your Callback URL under My Applications > (Select Your Application) > Settings > Callback URL
Next, grab your consumer key and secret codes, create twitter.php
configuration file into your fuel/app/config
directory. Here is a sample configuration file:
return array(
'production' => array(
// twitter api consumer configurations
'consumer_key' => 'SET_HERE_YOUR_CONSUMER_KEY',
'consumer_secret' => 'SET_HERE_YOUR_CONSUMER_SECRET',
),
'development' => array(
// twitter api consumer configurations
'consumer_key' => 'SET_HERE_YOUR_CONSUMER_KEY',
'consumer_secret' => 'SET_HERE_YOUR_CONSUMER_SECRET',
),
);
If you are gonna use twitter for personal stuff (for a static user) you can also declare your user access tokens for spesific enviroments. Your config file should be like this;
'production' => array(
// twitter api consumer configurations
'consumer_key' => 'SET_HERE_YOUR_CONSUMER_KEY',
'consumer_secret' => 'SET_HERE_YOUR_CONSUMER_SECRET',
// user
'user_token' => 'SET_HERE_YOUR_ACCESS_TOKEN',
'user_secret' => 'SET_HERE_YOUR_ACCESS_SECRET',
),
if ( ! Twitter::logged_in() ){
Twitter::login();
}
$user = Twitter::user();
if ( ! $user )
{
$user = Twitter::error_message();
}
echo "<pre>";
print_r($user);
echo "</pre>";
Check user currently logged in or not
if ( Twitter::logged_in() )
{
echo "User logged in."
}
Try to create session given configuration with parameters. You can check for the params from Twitter - OAuth/request_token
if( ! Twitter::logged_in() )
{
Twitter::login(array(
'x_auth_access_type' => "write"
));
}
Basically clear Twitter variables from session;
Twitter::logout();
Set user access tokens
Twitter::set_tokens(array(
'user_token' => 'SET_HERE_YOUR_ACCESS_TOKEN',
'user_secret' => 'SET_HERE_YOUR_ACCESS_SECRET',
));
Get user access tokens
$user_tokens = Twitter::get_tokens();
echo "<pre>";
print_r($user_tokens);
echo "</pre>";
/**
* Array
* (
* [user_token] => USER_ACCESS_TOKEN
* [user_secret] => USER_ACCESS_SECRET
* )
*/
GET request to given resource with parameters. To get more information about Resources and Parameters follow Twitter API Resources.
$user = Twitter::get('1.1/account/verify_credentials');
POST request to given resource with parameters. To get more information about Resources and Parameters follow Twitter API Resources.
$status = Twitter::post("1.1/statuses/update", array(
'status' => 'This is a supper dupper status! sent via FuelPHP Twitter Package'
));
Alias for Twitter::get('1.1/account/verify_credentials');
$user = Twitter::user();
Get specific time line status
//gets last 10 status from home_timeline
$home = Twitter::timeline("home", array(
'count' => 10
));
//gets user_timeline with twitter defaul parameters
$user = Twitter::timeline("user");
//gets mentions after status ID 12345
$mentions = Twitter::timeline("mentions", array(
'max_id' => 12345
));
More info about parameters, check Twitter statuses/home_timeline, Twitter statuses/user_timeline, Twitter statuses/mentions_timeline
Alias for Updates the authenticating user's current status
$status = Twitter::update("This is an another status.");
//or you can use with parameters
$status = Twitter::update(array(
'status' => "This is an another status.",
'trim_user' => 1
));
More info about parameters, check Twitter statuses/update
Destroys the status specified by the required ID parameter.
$status = Twitter::destroy(123456789);
Returns a collection of relevant Tweets matching a specified query.
$search_results = Twitter::search("SEARCH_KEY");
//or you can use with parameters
$search_results = Twitter::search(array(
'q' => SEARCH_KEY,
'count' => 10
));
More info about parameters, check Twitter search/tweets
Give the exact character size of status
$char_size = Twitter::char_size("café");
$strlen = strlen("café");
echo "café: " . $char_size . " != " . $strlen;
// result: café: 4 != 5
More info about counting characters, check Twitter Counting characters
Give the exact character size status with links
$status = "Test status: café https://github.com/tevfik6/fuel-twitter";
$safe_char_size = Twitter::safe_char_size($status);
$strlen = strlen($status);
echo $status." => " . $safe_char_size . " != " . $strlen;
//result:
// Test status: café https://github.com/tevfik6/fuel-twitter => 39 != 58
More info about conversition; check Twitter How do I calculate if a Tweet with a link is going to be over 140 characters or not?