Twitter OAuth the easy way – simple post to twitter script

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

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.

Addition:

Combining with bit.ly to shortened your link and limit your entry

function get_short_link($url) {
$bitly_login="<bit.ly login>";
$bitly_apikey="<bit.ly api key";
$api_call = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$bitly_login."&apiKey=".$bitly_apikey);
$bitlyinfo=json_decode(utf8_encode($api_call),true);
if ($bitlyinfo['errorCode']==0) {
return $bitlyinfo['results'][urldecode($url)]['shortUrl'];
} else {
return false;
}
}
// reduce text to twitter format
$length = strlen($text);
$ch = 70; //if the text is above 70 characters, the message is shortened
if ($length >= $ch) {
$foo1 = (substr("$text", 0, $ch));
$foo = "$foo1...";
} else {
$foo = $text;
}

Quoted from:

tips4php.net

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

http://bit.ly

Advertisements

Using jQuery with Other Libraries

Updates!!:

What can we do to fix this?

You can try moving the jQuery.js to top and than use jQuery.noConflict().

If that is not an option you can do the following –

IMPORTANT – Do not use [REPLACE ALL]

  • Edit prototype.js – find where it defines function $() and change the name to function $$$()
  • Still in prototype.js, carefully replace each occurrence of $(…) with $$$(…), but don’t touch anything which says $$(…)
  • Edit each of the other *.js files (e.g. effects.js) and carefully replace each occurrence of $(…) with $$$(…), but don’t touch anything which says $$(…).
  • That is it.

And just in case you have trouble doing the above, Following links point to already edited prototype.js and effects.js. Please remember all the references to these files must start with $$$ instead of $.

Download – Prototype.js

Download – Effects.js

quoted from: coolwebdeveloper.com

GENERAL

The jQuery library, and virtually all of its plugins are constrained within the jQuery namespace. As a general rule, “global” objects are stored inside the jQuery namespace as well, so you shouldn’t get a clash between jQuery and any other library (like Prototype, MooTools, or YUI).

That said, there is one caveat: By default, jQuery uses “$” as a shortcut for “jQuery”

OVERRIDING THE $-FUNCTION

However, you can override that default by calling jQuery.noConflict() at any point after jQuery and the other library have both loaded. For example:

 <html>
 <head>
   <script src="prototype.js"></script>
   <script src="jquery.js"></script>
   <script>
     jQuery.noConflict();

     // Use jQuery via jQuery(...)
     jQuery(document).ready(function(){
       jQuery("div").hide();
     });

     // Use Prototype with $(...), etc.
     $('someid').hide();
   </script>
 </head>
 <body></body>
 </html>

This will revert $ back to its original library. You’ll still be able to use “jQuery” in the rest of your application.

Additionally, there’s another option. If you want to make sure that jQuery won’t conflict with another library – but you want the benefit of a short name, you could do something like this:

 <html>
 <head>
   <script src="prototype.js"></script>
   <script src="jquery.js"></script>
   <script>
     var $j = jQuery.noConflict();

     // Use jQuery via $j(...)
     $j(document).ready(function(){
       $j("div").hide();
     });

     // Use Prototype with $(...), etc.
     $('someid').hide();
   </script>
 </head>
 <body></body>
 </html>

You can define your own alternate names (e.g. jq, $J, awesomeQuery – anything you want).

Finally, if you don’t want to define another alternative to the jQuery name (you really like to use $ and don’t care about using another library’s $ method), then there’s still another solution for you. This is most frequently used in the case where you still want the benefits of really short jQuery code, but don’t want to cause conflicts with other libraries.

 <html>
 <head>
   <script src="prototype.js"></script>
   <script src="jquery.js"></script>
   <script>
     jQuery.noConflict();

     // Put all your code in your document ready area
     jQuery(document).ready(function($){
       // Do jQuery stuff using $
       $("div").hide();
     });

     // Use Prototype with $(...), etc.
     $('someid').hide();
   </script>
 </head>
 <body></body>
 </html>

This is probably the ideal solution for most of your code, considering that there’ll be less code that you’ll have to change, in order to achieve complete compatibility.

Also see: Custom Alias

INCLUDING JQUERY BEFORE OTHER LIBRARIES

If you include jQuery before other libraries, you may use “jQuery” when you do some work with jQuery, and the “$” is also the shortcut for the other library. There is no need for overriding the $-function by calling “jQuery.noConflict()”.

 <html>
 <head>
   <script src="jquery.js"></script>
   <script src="prototype.js"></script>
   <script>
     // Use jQuery via jQuery(...)
     jQuery(document).ready(function(){
       jQuery("div").hide();
     });

     // Use Prototype with $(...), etc.
     $('someid').hide();
   </script>
 </head>
 <body></body>
 </html>

REFERENCING MAGIC – SHORTCUTS FOR JQUERY

If you don’t like typing the full “jQuery” all the time, there are some alternative shortcuts:

  • Reassign jQuery to another shortcut
    • var $j = jQuery;
    • (This might be the best approach if you wish to use different libraries)
  • Use the following technique, which allows you to use $ inside of a block of code without permanently overwriting $:
    • (function($) { /* some code that uses $ */ })(jQuery)
    • Note: If you use this technique, you will not be able to use Prototype methods inside this capsuled function that expect $ to be Prototype’s $, so you’re making a choice to use only jQuery in that block.
  • Use the argument to the jQuery(document).ready(function($) {})
DOM ready event:
    • jQuery(function($) { /* some code that uses $ */ });
    • Note: Again, inside that block you can’t use Prototype methods.

quotes: docs.jquery.com

onmouseover cursor using firefox: SOLVED!!

QUoting from David Harrison @webdeveloper.com:

It’s not hand, it’s pointer. M$ decided to throw caution to the wind and employ some MORE non-standard code. You don’t need to say onmouseover though since the rule will only apply to when the mouse is over the element anyway, so just do this:<some-tag style=”cursor:pointer;cursor:hand;”></some-tag>You have to specify pointer first, then hand. IE5 trips up because it doesn’t understand pointer (IE6 understands pointer though).
FYI, you didn’t reference the element properly either, should have been “this.style.cursor=’whatever’;”.

Thanks,