How to Update Twitter Timeline Using PHP

Guide on this article can be used to post Twitter using PHP.

First of all, download source code from http://phpfashion.com/twitter-for-php.

Extract downloaded file to your folder. Example: /public_html/twitter-php/.

Then we create index.php file with the following code:

<?php

$username = 'twitter-username';
$password = 'twitter-password';
$message = 'Hi, this is Twitter-PHP!';
$consumerKey = "abcd8jaC8exZnSnwCig";
$consumerSecret = "abcdtRishSKsbLboRShCTlh2qTtsFSrph2cvX0XqU";
$accessToken = "abcd29603-uhSN2DWztpdnN49JuLHexkKEHj5frJLczVnciaRw";
$accessTokenSecret = "abcdW3ZdJaljjFMcujZbDeY5rONbi6krf0scai7ETgA";

include("twitter.class.php");
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$status = $twitter->send($message);

echo $status ? 'OK' : 'ERROR';

?>

The first line is to setup variables: username, password, and message.

$username = 'twitter-username';
$password = 'twitter-password';
$message = 'Hi, this is Twitter-PHP!';

And the next part is to setup consumer key, secret, and token which can be retrieved from Twitter website.


$consumerKey = "abcd8jaC8exZnSnwCig";
$consumerSecret = "abcdtRishSKsbLboRShCTlh2qTtsFSrph2cvX0XqU";
$accessToken = "abcd29603-uhSN2DWztpdnN49JuLHexkKEHj5frJLczVnciaRw";
$accessTokenSecret = "abcdW3ZdJaljjFMcujZbDeY5rONbi6krf0scai7ETgA";

To get your key and token, go to https://dev.twitter.com/apps/new

Login with your Twitter username and password, and fill in the below form to create an application.

After finish with the form, you will get Consumer Key and Consumer Secret.

Next, click Create my access token button to get access token.

Write down all the consumer key, secret, and token. Make sure you put on the variables in our PHP code above.

The next code is to call twitter.class.php , create Twitter instance, and callĀ  send() method to post to Twitter timeline.

include("twitter.class.php");
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$status = $twitter->send($message);

The last code is to get the status whether it is success or not.

echo $status ? 'OK' : 'ERROR';

To test the code, open browser and go to http://yourdomain/twitter-php/index.php, also check your Twitter timeline for the message.

Reference: http://phpfashion.com/twitter-for-php

Leave a Reply

Your email address will not be published. Required fields are marked *