Today's Question: 

Hide

Different programming are similar in helping people build staff work as people want them to. But they all have their own features which differentiate them from other programming languages. The language type may be different, for example Java is OOP, some may have different syntax. A programming motto usually can best describe the characteristic of the programming language. For example, "Write once Run everywhere" for Java.Do you know any other programming language motto?
Do you know the mottos of programmig languages?
Description My Answer

Technical Article => Other =>  Other

Twitter OAuth the easy way – simple post to twitter script

Source : tips4php.net    Date : 2012-02-25 12:51:07  

After Twitter introduced mandatory authentication with OAuth, many of the current scripts for posting content to Twitter don’t work anymore.

OAuth can be great for more advanced authentication, but for a simple post to twitter script, it seems like a little overkill.

In this post you’ll learn how to create a simple script that uses a quick and dirty version of OAuth for posting new tweets to Twitter.

How to create a simple script

Simplified, Twitter OAuth involves sending both application tokens and user tokens back and forth between your site and Twitter.

If you want to authenticate multiple users, you need a full OAuth implementation, but if you only want a script that sends tweets from your site or application, the good news is that all the authentication tokens and keys can be reused, which makes it possible to build a very simple script, as long as you just get the required tokens and keys once .

To get the required key and tokens you need to carefully follow the next steps

Step 1 – Register your application

First you need to register your application at Twitter here.

Please notice that you need to log in to Twitter at the start of the Application  registration process. The account that you’re logging in to, is naturally also the twitter account that you’re application can post tweets to.

Signup form for twitter applications

Filling in the form is pretty straightforward.

The only special requirement for our purpose is set Default Access type to “Read & write”, so your application is allowed to post tweets to twitter

Step 2 – Consumer secret and Consumer key

When your application is registered by Twitter, you also have all the required keys for your script.

You find the consumer keys here:

View Your Applications -> Edit Details

The consumer keys can be found at the last part of the page

Step 3 – Access token and Access token secret

You find the Access token and Access token secret by clicking on the “My Access token” link in the right menu.

When you have the four keys/tokens:

  • Consumer secret
  • Consumer key
  • Access token
  • Access token secret

You’re ready to proceed

Step 4 – Twitter OAuth class

To connect to twitter using OAuth we’ll be using the brilliant Abraham Twitter OAuth class. The Abraham twitter OAuth class has a lot of overhead enabling more advanced authentication than is required for this script. You only need the two files “OAuth.php”  and “twitteroauth.php”.

You can download the two files directly here.

When you have downloaded the files, and uploaded them on your server, you’re ready for the last step

Step 5 – Post to twitter script

Having the required access tokens, keys and the Twitter OAuth class, it’s a piece of cake to build a script that posts messages to twitter.

You just need to insert the required keys and the path to the twitteroauth.php file in the script below, and then you’re up and running.

01<?php
02$consumerKey    = '<insert your consumer key';
03$consumerSecret = '<insert your consumer secret>';
04$oAuthToken     = '<insert your access token>';
05$oAuthSecret    = '<insert your token secret>';
06 
07require_once($_SERVER['DOCUMENT_ROOT'].'/<insert path to twitteroauth>/twitteroauth.php');
08 
09// create a new instance
10$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
11 
12//send a tweet
13$tweet->post('statuses/update', array('status' => 'Hello World'));
14?>

Conclusion

The process for getting this script up and running might be a little  complex, but you only have to go through the process once, and then you have a very simple to use script for sending tweets to twitter from a PHP script.

Source:http://tips4php.net/2010/12/twitter-oauth-the-easy-way-simple-post-to-twitter-script/

Save as PDF Mark as read Mark as important
By clicking the "Mark as read" button, this article will be marked as read. It will be removed from the homepage's latest news and the article list on the "Technical article" page in following visits and it will be put to your read list which you can find in "Amin->Article read list". There you can unmark the read articles.
By clicking the "Mark as important" button, this article will be put to your important article list which you can find in "Amin->Article important list". Later when you want reread this article, it's easier for you to find it by checking the "Article important list".

Tags : Twitter,OAuth,PHP,Auto tweet   Read(3316) Comment(0) Version(0)

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious 

 Previous : How To Find Link Prospects Without Using Google
 Next : Is 99.8% Secure Secure?

  ::Related Articles

  ::Comment Zone  (Total 0 comment)

No comment for this article.

  ::Comment

Nickname  
Email 
Comment

:: Other versions

No other versions available yet.

:: Recent articles

:: Most read

:: Contribute

Want to share with the world your understanding about technology? Want to record the process you solve a technical problem? Want to make the world benefit from your understanding and solution? Write them down. You make the world better, the world makes us better.

Write article

:: Find us

Back to top