In order to post twits to Twitter using Twitter APIs, you would first need to go to Twitter Dev site http://dev.twitter.com/apps.
You can easily follow the screen instructions to create a new application and get the tokens and secret key, which are required to access the APIs. It is therefore recommended to store these values inside a PHP constant file.
1 2 3 4 5 6 | $twitter_apikey = 'api key'; $twitter_apisecret = 'api secret'; $twitter_ownerid = 'owner id'; $twitter_accesstoken = 'access token'; $twitter_accesstokensecret = 'access token secret'; $twitter_screenname = 'doctorzlai'; |
$twitter_apikey = 'api key'; $twitter_apisecret = 'api secret'; $twitter_ownerid = 'owner id'; $twitter_accesstoken = 'access token'; $twitter_accesstokensecret = 'access token secret'; $twitter_screenname = 'doctorzlai';
Now, the next thing is to use some PHP Library for Twitter. The best known is the OAuth, which can be found [here].
Posting a twit is thus easy using such library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // helloacm.com require('twitter/TwitterOAuth-1/TwitterOAuth/TwitterOAuth.php'); require('twitter/TwitterOAuth-1/TwitterOAuth/Exception/TwitterException.php'); require('twitter/TwitterOAuth-1/config.php'); use TwitterOAuth\TwitterOAuth; date_default_timezone_set('UTC'); $config = array( 'consumer_key' => $twitter_apikey, // API key 'consumer_secret' => $twitter_apisecret, // API secret 'oauth_token' => $twitter_accesstoken, // not needed for app only 'oauth_token_secret' => $twitter_accesstokensecret, 'output_format' => 'object' ); $tw = new TwitterOAuth($config); function postTwitter($msg, $url = '') { $msg = trim($msg); $len = strlen($msg); if ($len <= 1) return false; $url = trim($url); if ($len >= 139) { $msg = substr($msg, 0, 139); } $params = array( 'status' => $msg." ".$url ); global $tw; return $tw->post('statuses/update', $params); } |
// helloacm.com require('twitter/TwitterOAuth-1/TwitterOAuth/TwitterOAuth.php'); require('twitter/TwitterOAuth-1/TwitterOAuth/Exception/TwitterException.php'); require('twitter/TwitterOAuth-1/config.php'); use TwitterOAuth\TwitterOAuth; date_default_timezone_set('UTC'); $config = array( 'consumer_key' => $twitter_apikey, // API key 'consumer_secret' => $twitter_apisecret, // API secret 'oauth_token' => $twitter_accesstoken, // not needed for app only 'oauth_token_secret' => $twitter_accesstokensecret, 'output_format' => 'object' ); $tw = new TwitterOAuth($config); function postTwitter($msg, $url = '') { $msg = trim($msg); $len = strlen($msg); if ($len <= 1) return false; $url = trim($url); if ($len >= 139) { $msg = substr($msg, 0, 139); } $params = array( 'status' => $msg." ".$url ); global $tw; return $tw->post('statuses/update', $params); }
It is worth to mention that the Twitter APIs have the rate limit, which means you cannot use too many calls within a short time per IP address. Although, there is a 140 character limit per twits but the URL length is kinda excluded.
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Coding Exercise - Source Codes and Binaries
Next Post: Bouncing Balls Animation Made in Processing Programming Language