Manually post to social networks is probably better than to use an automated method. But, if you post many new topics every day, automated post is practically inevitable.
Twitter, as almost any social network, brings an Application Programming Interface, Twitter API, in order to communicate with it programmatically. You cannot use Twitter API directly (using your username and password), but you have to create a Twitter App. Creating a Twitter App is easy (see below). After you create your app, you get ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret and you have to use them as credentials to communicate using Twitter API.
How to Create a Twitter app
Step 1 - Go to https://dev.twitter.com/apps and sign in with your account
Step 2 - Press 'Create a new application' button
Step 3 - Complete the required fields and press 'Create your Twitter application' (in the bottom of the screen)
Step 4 - Go to 'Settings' TAB and set Application Type to 'Read and Write'. Then press 'Update this Twitter application's settings'
Step 5 - Go to Details TAB and press 'Create my access token' button
Step 6 - Go to oAuth tool TAB and get your access tokens
Which PHP library for Twitter to select?
You may use any library available (in PHP section) of twitter libraries.
I tested many of these libraries. The are all working well. I finally selected Codebird-php, because
- it is well written and documented
- you can easily post an image with this library
Actually, you need two files
- codebird.php (the main class)
- cacert.pem (Validate Twitter SSL certificate)
Get and put them in the same directory.
- PHP 5.3.0 or higher
- php CURL extension (install to Debian/Ubuntu etc with
apt-get install php5-curl)
- php OpenSSL extension (already installed in most systems)
Post single tweet using php
Here is an example to post a single tweet
// require codebird require_once('/path/to/codebird_php_v2.4.1/codebird.php'); \Codebird\Codebird::setConsumerKey("your_ConsumerKey", "your_ConsumerSecret"); $cb = \Codebird\Codebird::getInstance(); $cb->setToken("your_AccessToken", "your_AccessTokenSecret"); $params = array( 'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter' ); $reply = $cb->statuses_update($params);
Post tweet with image using PHP
Here is an example to post a tweet with image
// require codebird require_once('/path/to/codebird_php_v2.4.1/codebird.php'); \Codebird\Codebird::setConsumerKey("your_ConsumerKey", "your_ConsumerSecret"); $cb = \Codebird\Codebird::getInstance(); $cb->setToken("your_AccessToken", "your_AccessTokenSecret"); $params = array( 'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter', 'media' => '/path/to/picture.png' ); $reply = $cb->statuses_updateWithMedia($params);
Twitter 140 characters restriction
Although the total length is more than 140 chars, you can successfully tweet DIRECTLY, something like this
Automatically Post on Twitter with PHP using Codebird - Step by step tutorial http://www.pontikis.net/blog/auto_post_on_twitter_with_php #php #twitter
because, Twitter will shorten the URL for you, so the total lenght will become less than 140.
But, you cannot do it using the above code. So, keep your automatic tweets length less than 140 characters (use fewer words or shorten URL)
Automatically Post on Twitter with PHP using Codebird http://goo.gl/OZHaQD #php #twitter