Twitter API: tweet with PHP

Today’s web is about sharing. My facebook API / movie experiment Sharemovi.es is up and running. It needs some tweaking but bit by bit..  What I did find important was a live stream. Of course you can go the old-school route of mail updates, but today updates are more efficient via facebook and twitter.

As I played already with the facebook API, today it was time to do some research on Twitter’s API. Purpose: push new movie reviews to twitter! This way people can follow sharemovies and they get new movies automatically pushed into their stream. (Updates on the app are however posted to facebook via the activity stream)

There are some tutorials that suggest this method:

<?php
include '../../twitter.php';
$message = 'New movie ...';
$url = 'http://twitter.com/statuses/update.xml';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer)) {
 echo "error!";
} else {
 echo 'success!';
}

.. but this method does not work anymore because twitter is now using Oauth.

So I followed this helpful article:

1. Register an app: http://dev.twitter.com/apps/new. This is quite common; Facebook asks the same.
You need the read & write permissions to post to your twitter account

2. You get 4 API keys. Download the Abraham Twitter OAuth class to connect to twitter using OAuth and fill in the keys.

3. including “OAuth.php”  and “twitteroauth.php” the rest is easy

File tweet.php

<?php
$consumerKey    = '<insert your consumer key';
$consumerSecret = '<insert your consumer secret>';
$oAuthToken     = '<insert your access token>';
$oAuthSecret    = '<insert your token secret>';
require_once('twitteroauth.php');
// twitteroauth.php points to OAuth.php
// all files are in the same dir
// create a new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
 ?>

This makes the twitter instance which you can use to post updates to your stream (the user account that registers the app, is used to post the updates to!) :

// post to twitter
include 'tweet.php';  // the file where the tweet instance is created
$statusMessage = 'Movie added: '.$title. ' -> ' . $message;
$tweet->post('statuses/update', array('status' => $statusMessage));

So when adding this bit of code at the point where somebody adds a movie, it posts the new title + URL to the @sharemovies twitter account. Following this user and you get updated about new movies in your twitter stream :)

Movie stream

Create your Movie stream with twitter API

I probably deploy the same with Facebook over time, but it is always good to play with various technologies.

==

If you like this article, please share it to Facebook or Twitter with the corresponding buttons at the top and bottom of this article ..


You might also like:



Enjoyed this article?

Share on Google+

This entry was posted in Social media and tagged , , , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • Sgacheru

    I like the way you experiment and then share about it. Keep it up man

    • Anonymous

      It’s fun and never gets boring!
      Thanks Stephen for sharing and your feedback on sharemovi.es

  • Adrian

    thanks a lot for your script! works perfect !!!

    • Anonymous

      you are welcome. you used it somewhere publicly?

  • http://twitter.com/bikt87 bikt87

    ádsdsadsadsa

  • pawan damase

    hi can you please tell us how to change the permission to “read & write”

  • Danoski74

    This does not work for me. Where is twitter.php

    • http://bobbelderbos.com/ Bob Belderbos

      Has been some time I wrote this, I will check it out for you

  • Scott Rowley

    1) I keep getting a 401, I have the correct 4 API pieces and my app is set to full permissions (read, write, direct messages).  
    2) You have a typo in the above code.  in the require_once for twitteroauth.php you are missing the starting ‘Any help?  This is driving me nuts! Thanks!

    • Scott Rowley

      Nevermind,  I deleted the app and then recreated it.  It appears (as noted by others) that Twitters API has an issue when you move an app from read only to read/write.

      • http://bobbelderbos.com/ Bob Belderbos

        OK, I wrote this long ago, so you need read+write from the very start, right? Thanks for the feedback

    • http://bobbelderbos.com/ Bob Belderbos

      thanks for catching the typo, corrected it

  • My Links

  • Post categories

  • Bob’s Reading List

  • Some of my Apps

    hi folks, what are you reading?

    sharemovi.es: tell your friends about your favorite movies

    Friends Jukebox | Create a Jukebox based on the Music your Friends like!

    get to know the world ...

    keep the tweets you care about ...