Previously we looked into how to setup a our own Twitter application using the Twitter API. That method of Twitter integration was slightly complicated in that it required requesting and utilizing access keys and tokens and a basic understanding of at least one well-known scripting language. Twitter Web Intents is another option Twitter offers to integrate Twitter features into any website or application. It requires no prior authorization to use, no complicated keys or tokens and can be setup in less time than it takes to copy and paste a few lines of html.
Twitter Web Intents are great for adding Twitter functionality to plain text links or custom buttons to better match your website design. In addition, they are mobile friendly out-of-the-box and require very little programming experience to understand and customize.
We’ll need a little bit of server-side scripting to put together the URL and parameters needed to build links to point to your Twitter screen name and the URL of the page being tweeted. An example in PHP is included below:
<?php $http = $_SERVER['HTTPS'] = 'on' ? 'https://':'http://'; $url = urlencode($http.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); $screen_name = 'twAPIdev'; // use your Twitter screen name $hashtags = "twitterapi,coding"; // comma separated list of hashtags (without #) to automatically be inserted into the tweet ?>
The most basic usage allows you to add a Tweet This link or button to pages on your site. Clicking the link will pop up a small window allowing your readers to login to their Twitter account and tweet a comment and link back to your page. If you specify a comma separated list of keywords for the $hashtags variable (above), the tags will be automatically included in the body of the tweet. Users may choose to edit or remove these tags before posting the tweet, so they are more of a suggestion, but great for creating your own unique hashtags.
<a href="http://twitter.com/intent/tweet?url=<?php echo $url; ?>&hashtags=<?php echo $hashtags; ?>">Tweet This</a>
To use an image in the link instead of the Tweet This text, simply replace Tweet This with a standard HTML image. Twitter also provides a list of their standard icons for your usage.
<img src="http://full-path-to-image.jpg" alt="Tweet This" border="0" />
Similarly, the link example below will incorporate a Follow Me button or link on the pages of your site. Clicking the link will pop up a window showing basic profile information and allow your readers to quickly follow your Twitter account without leaving your site.
<a href="http://twitter.com/intent/user?screen_name=<?php echo $screen_name; ?>">Follow Me</a>