I have received a tutorial requests from my reader Sahil Dua, he asked me how to create php tweet to download script. And I go to google for this, but did not find any useful article on Google first page results. So here is the solution. This post will explain you how to create a PHP Tweet to Download Script. In this script I m using Twitter OAuth
First of all you need to create a twitter application for this. So please follow following steps:-
- First go to https://dev.twitter.com/ and login with your twitter account from top right hand side Sign in link.
- Now go to apps section via https://dev.twitter.com/apps
- Now click on “Create a new application” button
- Now fill all the details, remember one thing, in Callback URL textbox put “http://yourwebsite.com/foldernamewhereyouputallthesourcecode/getTwitterData.php” and click to “Create your Twitter application” button.
- After create the application go to Setting tab, Application Type section. Here Access must be “Read and Write“
- Now go to Detail tab and copy the “Consumer key” and “Consumer secret” value.
- Now your application is ready, and open the downloaded the source code.
Now here is the code flow structure:
config/constants.php :- contain all the constants (Twitter keys, Tweet Message, Downloaded Link, Website Path etc). Put copy Consuner Key value in “$consumer_key” variable and Consumer secret value in “$consumer_secret” and set your website path in “SITE_PATH” field.
index.php In this file firstly we will check “$_SESSION['access_token']” exist or not. If it’s value exist, than pass it to hidden variable “twitterOauthSession” and ”twitterOauthTokenSecret”.
In this page if ”$_SESSION['showDownload']” is set and contain a value “done” only than you will see a download link otherwise you will see “Tweet For Us To Download the Source Code” button.
If not exist, than call a file “login-twitter.php” by window.open method. This file call the twitter api, after twitter api verification, request go back to “getTwitterData.php” . This file first analyse the twitter response, and set “$_SESSION['oauth_token']” variables and call the “twitterPost.php” file by ajax, after alert the response , ”self.close()” to close the current open window, and “window.opener.location=’index.php’” reload the parent window and you after page reload you seen a download link.
With this “PHP Tweet to Download “ is complete! All The Best and Happy Coding….