16

I've seen recently a few times when companies offer either a download link or a coupon code for people that will tweet about it. It is all automated and I would like to do something like this but I'm not quite clear on the process.

Usually you go to a page with a link. You click on a link and it asks if you would give permission for some Tweeter app to connect to your profile. You grant permission and it brings up a pre-composed twitter message, something like: "I just got a coupon to try out..." Once the twitter message is sent, you are re-directed to a page where you find your coupon code.

I use PHP and can write whatever is needed to get this done.

Anyone knows how it is all done?


Update:

OK, I downloaded http://code.google.com/p/oauth-php/still not clear what to do next.

I am trying to figure out startuing with this page: http://code.google.com/p/oauth-php/wiki/ConsumerHowTo#Two-legged_OAuth

The way I understand the process the following should take place in this order:

  1. I need a link somewhere on my site that will have an offer: "Send a Twit and receive a coupon", right?

  2. Once they click on the offer link, how do I populate a message into their twitter update window: "Trying out this tool for free: http"//mylink.com"?

  3. Once they submit Twitter post, I suppose I will have some settings that will redirect back to my site to a page with a coupon.


UPDATE: switched to twitteroauth, but still need help...

2
  • 5
    "I use PHP and can write whatever is needed to get this done." - Go for it: Install one of the many twitter PHP APIs, offer your users to use it with their credential with some little PHP script. Post tweets for them within that little PHP script, redirect them on success via sending the location header to your coupon script. Done.
    – hakre
    Jun 28, 2011 at 14:49
  • That "little PHP script"... I'd love to see an example of that. What is it suppose to do? Thanks.
    – santa
    Jun 28, 2011 at 15:19

4 Answers 4

8
+25

Basically, you have to create an app that uses the OAuth API to connect to twitter and post a tweet. This takes several steps (assuming you've registered your application with Twitter, giving you a key and secret):

  1. Build TwitterOAuth object using client credentials.
  2. Request temporary credentials from Twitter.
  3. Build authorize URL for Twitter.
  4. Redirect user to authorize URL.
  5. User authorizes access and returns from Twitter.
  6. Rebuild TwitterOAuth object with client credentials and temporary credentials.
  7. Query Twitter API.

Using TwitterOAuth, it would look something like this:

<?php
  require_once 'TwitterOAuth.php';
  define('THIS', 'http://example.org/tweet.php'); // Absolute URI to script

  if(isset($_GET['callback']) {
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, 
                      $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    $connection->post('statuses/update', array('status' => 'Coupon message'));
    header('Location: coupon.php'); // Supplies user with coupon
  } else {
    $connection = new TwitterOAuth('key', 'secret'); // Key & secret from Twitter
    $temporary_credentials = $connection->getRequestToken(THIS.'?');
    $redirect_url = $connection->getAuthorizeURL($temporary_credentials, FALSE);
    header('Location: '.$redirect_url.'&callback');
  }
?>

(Note that the above code is untested; read the documentation of TwitterOAuth before doing things yourself.)

9
  • I already have an app setup with the Twitter for this site, can I use it or do I need to setup a new one?
    – santa
    Jul 11, 2011 at 23:52
  • @santa: You should be able to use the same key/secret pair here.
    – You
    Jul 12, 2011 at 0:01
  • OK, I followed documentation and now I have a start page, where a user can click on a "Login w/ Twitter" button, redirected to Twitter. After successful login there, they are redirected back to my redirect.php page which checks for successful login and if true -- sends them to a specified page. At which point I get them to twit and upon successful post of the twit redirect them back to coupon code page? Still kinda unclear on the process.
    – santa
    Jul 13, 2011 at 17:58
  • @santa: That's pretty much exactly how it goes.
    – You
    Jul 13, 2011 at 18:42
  • I understand, but I am not looking for them to access my page after successful login on Twitter. I need them to twit the offer and then get redirected to a coupon. How do I do that? I feel like I am 75% there but not sure what the remaining 25% is.
    – santa
    Jul 13, 2011 at 18:53
3

For the more lazy of us ;) This makes a button to pay with a tweet (like so enter image description here) and links to an URL of your choice afterwards. I guess you could reach most off your goals with this (though coupon codes that are personal might be tricky).

1
  • This is cool! I suppose, until I figure out how to make my code work I'll opt in for this app.
    – santa
    Jul 15, 2011 at 12:24
1

You could use Abraham's Twitter OAuth Library, I would suggest it as it makes the OAuth flow incredibly easy.

What you'll want to do is make a link like "Tweet about this to get a free download" or whatever. That link will redirect them to authorize your Twitter Application, when the callback comes back to your site, you request an Access Token and save it. This page should have a text box pre-populated with your message that posts to your site with the message that they want to send. You'll do an API call to post the message with the access token you received. Once you make the call and post the tweet, show them the page for the download.

Take a look at the TwitterOAuth Library Documentation as it has detailed examples on how to use the library to make the calls you're looking for.

1
  • I have used that library, but still struggle with getting a tweet posted.
    – santa
    Jul 14, 2011 at 17:51
0

A little PHP script and what it does:

// php.ini::session.auto_start=1
include('commands.php');
$command = get_command_from_request();
switch($command) {
  case 'tweet': 
      command_tweet(); break;
  case 'redirect':
      command_redirect(); break;
  default:
    command_default();
}

Keep in mind that you need to implement the mapping of a command to the request yourself (implementation of get_command_from_request), as well for the concrete commands.

Tweeting depends on the API you use. Any of the many APIs in question has this as an example.

Next to that you need to build a HTML form to gather the data you need to tweet (the API keys and such). If you have no clue how data goes into your script, checkout the PHP manual about forms.

So let's assume the command_default to display the form as well as the it's verification inside command_tweet and the get_command_from_request is done now. The rest is doing the redirect to your secret coupon script.

As you have not shared much about your secret coupon script, a redirect is probably enough. A nice function for doing a HTTP redirect in PHP is http_redirect().

1
  • I've ended-up implementing TwitterOAuth, and although everything seem to work I don't see a message posted.
    – santa
    Jul 15, 2011 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.