Mar 022013
 

If we want to post from twitter to other social media, we have to figure out how to read tweets – and I want to do it using PHP.

The following steps are needed to make ik possible to read tweets from Twitter with PHP:

  • Create a Twitter application
  • Install PHP libraries for accessing Twitter
  • Write some code to read tweets

Create a Twitter application

A Twitter application will provide you with a set of access codes that grant you access to the Twitter API, and thereby grants you access to your tweets.
Creating a Twitter app is quite easy: go to https://twitter.com/apps, create a new app, fill in some details, and generate the access tokens.

This is the screen for a new app:

read tweets: create your app

This is easy; give your app a name, give it a description, fill in a website. The website is in fact not important for our usage, nobody will actually visit a website, our program will access Twitter, and that does not have to be from our website.

The same is true for the Callback URL: there is no user interaction and therefore this URL will not be called.
Further down the screen, agree to the rules, fill in the Captcha, and submit the form.

If all goes well, you get a screen like this:

read tweets: create tokens

Now you have a Consumer key and Consumer secret for your application; however you also need access tokens. Click on ‘Create my access token’, and you get the following extra info (maybe you have to refresh the screen to actually make the access tokens visible):

read tweets: all tokens available

So, now we have everything we need for accessing twitter feeds:

  • Consumer key
  • Consumer secret
  • Access token
  • Access token secret

Do the best you can to keep your secrets secret.

 

Install PHP libraries for accessing Twitter

There is no point in communicating directly with the Twitter API, libraries have been written for you to do that. It makes our life a lot easier.
I used the libraries from twitter-async: https://github.com/jmathai/twitter-async. You only need

  • EpiCurl.php
  • EpiOAuth.php
  • EpiSequence.php
  • EpiTwitter.php

I put them in a directory called lib, and included them in my script tweets.php
I put the access keys in a separate directory called cfg, and a file called secret.php
So now we can actually start coding.

 

Write some code to read tweets

Let’s create the file with the secrets first. Make sure that the file readable only for you and your application.

cfg/secret.php

  1. <?php
  2.  
  3. $consumer_key    = 'your consumer key';
  4. $consumer_secret = 'your consumer secret';
  5. $access_token    = 'your access token';
  6. $access_secret   = 'your access secret';
  7.  
  8. ?>

tweets.php

  1. <?php
  2.  
  3. include 'lib/EpiCurl.php';
  4. include 'lib/EpiOAuth.php';
  5. include 'lib/EpiTwitter.php';
  6. include 'cfg/secret.php';
  7.  
  8. # Create a Twitter object
  9. $twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $access_token, $access_secret);
  10.  
  11. # Get tweets
  12. $status = $twitterObj->get('/statuses/user_timeline.json');
  13. $response = $status->response;
  14. var_dump($response);
  15.  
  16. ?>

Thats it! Well, there is no error handling in this script; if something goes wrong, the libary will throw an exception; you can of course catch the exceptions, but for simplicity I won’t.

Run the script like ‘php tweets.php’ and see what happens.
I guess you expect the text of a tweet, maximum length 140 character.
Well, you will be surprised, instead you get an enormous structure with a lot of information, for example:

  1. array(10) {
  2.   [0]=>
  3.   array(20) {
  4.     ["created_at"]=>
  5.     string(30) "Sat Mar 02 11:20:10 +0000 2013"
  6.     ["id"]=>
  7.     int(307812569148706816)
  8.     ["id_str"]=>
  9.     string(18) "307812569148706816"
  10.     ["text"]=>
  11.     string(43) "Link naar mijn blog: https://t.co/5eqiHRX42g"
  12.     ["source"]=>
  13.     string(3) "web"
  14.     ["truncated"]=>
  15.     bool(false)
  16.     ["in_reply_to_status_id"]=>
  17.     NULL
  18.     ["in_reply_to_status_id_str"]=>
  19.     NULL
  20.     ["in_reply_to_user_id"]=>
  21.     NULL
  22.     ["in_reply_to_user_id_str"]=>
  23.     NULL
  24.     ["in_reply_to_screen_name"]=>
  25.     NULL
  26.     ["user"]=>
  27.     array(37) {
  28.       ["id"]=>
  29.       int(131261475)
  30.       ["id_str"]=>
  31.       string(9) "131261475"
  32.       ["name"]=>
  33.       string(13) "Sietse Visser"
  34.       ["screen_name"]=>
  35.       string(10) "vogon1test"
  36.       ["location"]=>
  37.       string(0) ""
  38.       ["url"]=>
  39.       NULL
  40.       ["description"]=>
  41.       string(0) ""
  42.       ["protected"]=>
  43.       bool(false)
  44.       ["followers_count"]=>
  45.       int(1)
  46.       ["friends_count"]=>
  47.       int(0)
  48.       ["listed_count"]=>
  49.       int(0)
  50.       ["created_at"]=>
  51.       string(30) "Fri Apr 09 19:19:04 +0000 2010"
  52.       ["favourites_count"]=>
  53.       int(0)
  54.       ["utc_offset"]=>
  55.       int(3600)
  56.       ["time_zone"]=>
  57.       string(9) "Amsterdam"
  58.       ["geo_enabled"]=>
  59.       bool(false)
  60.       ["verified"]=>
  61.       bool(false)
  62.       ["statuses_count"]=>
  63.       int(10)
  64.       ["lang"]=>
  65.       string(2) "nl"
  66.       ["contributors_enabled"]=>
  67.       bool(false)
  68.       ["is_translator"]=>
  69.       bool(false)
  70.       ["profile_background_color"]=>
  71.       string(6) "C0DEED"
  72.       ["profile_background_image_url"]=>
  73.       string(47) "https://a0.twimg.com/images/themes/theme1/bg.png"
  74.       ["profile_background_image_url_https"]=>
  75.       string(49) "https://si0.twimg.com/images/themes/theme1/bg.png"
  76.       ["profile_background_tile"]=>
  77.       bool(false)
  78.       ["profile_image_url"]=>
  79.       string(78) "https://a0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png"
  80.       ["profile_image_url_https"]=>
  81.       string(80) "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png"
  82.       ["profile_link_color"]=>
  83.       string(6) "0084B4"
  84.       ["profile_sidebar_border_color"]=>
  85.       string(6) "C0DEED"
  86.       ["profile_sidebar_fill_color"]=>
  87.       string(6) "DDEEF6"
  88.       ["profile_text_color"]=>
  89.       string(6) "333333"
  90.       ["profile_use_background_image"]=>
  91.       bool(true)
  92.       ["default_profile"]=>
  93.       bool(true)
  94.       ["default_profile_image"]=>
  95.       bool(true)
  96.       ["following"]=>
  97.       bool(false)
  98.       ["follow_request_sent"]=>
  99.       bool(false)
  100.       ["notifications"]=>
  101.       bool(false)
  102.     }
  103.     ["geo"]=>
  104.     NULL
  105.     ["coordinates"]=>
  106.     NULL
  107.     ["place"]=>
  108.     NULL
  109.     ["contributors"]=>
  110.     NULL
  111.     ["retweet_count"]=>
  112.     int(0)
  113.     ["favorited"]=>
  114.     bool(false)
  115.     ["retweeted"]=>
  116.     bool(false)
  117.     ["possibly_sensitive"]=>
  118.     bool(false)
  119.   }
  120. }

Most likely you get 20 records because by default, the API returns the last 20 tweets.
Because I used

  1. $status = $twitterObj->get('/statuses/user_timeline.json');

I only get my own tweets. You can get a lot more from Twitter using the API than just your own tweets; have a look at the Twitter API at https://dev.twitter.com/docs/api/1.1

Yes, the tweet texts are somewhere in the structure returned by Twitter. To show them, use this:

  1. <?php
  2.  
  3. include 'lib/EpiCurl.php';
  4. include 'lib/EpiOAuth.php';
  5. include 'lib/EpiTwitter.php';
  6. include 'cfg/secret.php';
  7.  
  8. # Create a Twitter object
  9. $twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $access_token, $access_secret);
  10.  
  11. # Get tweets
  12. $status = $twitterObj->get('/statuses/user_timeline.json');
  13. $response = $status->response;
  14.  
  15. # Show actual texts of the tweets
  16. foreach ($response as $tweet) {
  17.     echo $tweet["text"] . "\n";
  18. }
  19.  
  20. ?>

This will show your last 20 tweets.

  One Response to “How to read tweets from Twitter using PHP”

  1. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you
    would have some experience with something like this. Please let me know if you run into
    anything. I truly enjoy reading your blog
    and I look forward to your new updates.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Social Widgets powered by AB-WebLog.com.