a digital blog

AJAX, Java Script, JQuery, PHP, Tutorial

PHP Tweet to Download Twitter Script

PHP Tweet to Download Twitter Script

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


Demo & Download Link


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.

On click to “Tweet For Us To Download the Source Code” button, “tweetForUs” javascript function called. Firstly it will check if value exist in hidden field  ”twitterOauthSession” or not.

If exist than call to another javascript function “post_to_twitter” and this function call a file “twitterPost.php” by ajax method.  ”twitterPost.php” file contain the tweet post message and call the twitter api to post the tweet and contain response in $response variable. Here if this variable could not contain any error only than “$_SESSION['showDownload']” value contain a value “done”, and return a message “Your text is successfully posted on your twitter wall.” to “post_to_twitter” javascript function. Here function alert that message and refresh the current page. And as per my above point right now “$_SESSION['showDownload']” is set and contain a value “done”, that why you are seen a download link.

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….

Vishal Gupta

Follow     Follow         Email at vishalgupta@91weblessons.com


  1. Thank you so much for bringing this to our minds eye. This is a good article piece with a lot of information, great content!

  2. Fantastic goods frоm уou, man. I have understand yоur stuff previous
    to andd you’re just extremely wonderful. I aсtuallу like what
    you have acԛuired here, certainly like what you’re saying аnd
    the wayy in which you say it. Yоu make it enjoyable andԁ you still care for tο keep it sensible.
    I can not waіt to read far more from you. This is actually a
    gгeat site.

  3. Richard

    This is really a great tutorial

    And Great if you can show us the same thing for Facebook share or Like-button please……

  4. Jared Erickson

    Excellent job, as always!

    Thanks for the article i find it really useful i may use it in any of my future projects

  5. Ashe

    Keep up the good work!

  6. Developer

    Outstanding Scripts, You make my day…….


Copyright © 91 Web Lessons - a digital blog