A Simple Way to Get User Timelines with Twitter API version 1.1 & PHP

A simple way to get user timelines in PHP is to use “The first PHP Library to support OAuth for Twitter’s REST API” by Abraham Williams. The library supports version 1.1 of the Twitter API.

Here is a sample PHP code with the library to get most recent tweets posted by @TwitterEng (the official account for Twitter Engineering).

Sample Code

<?php

require __DIR__ . '/lib/twitteroauth/twitteroauth.php';

define('CONSUMER_KEY', 'your_consumer_key');
define('CONSUMER_SECRET', 'your_consumer_secret');
define('ACCESS_TOKEN', 'your_access_token');
define('ACCESS_TOKEN_SECRET', 'your_access_token_secret');

$toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

$user = 'TwitterEng';

$timeline = $toa->get('statuses/user_timeline', array('screen_name' => $user));

foreach ($timeline as $i => $tweet) {
    echo "$i: $tweet->text" . PHP_EOL;
}

Output

0: Learn about how migrating to @netty_project 4 made our garbage collection more efficient: https://t.co/50uSgxieXc
1: We worked with 3 amazing students as a part of the Google Summer of Code. Check out their projects here: https://t.co/VQZYFythCR
2: "Java and Scala let Twitter readily share and modify its enormous codebase across a team of hundreds of developers." http://t.co/SvrZyeaSCL
3: Learn about @ParquetFormat in depth and columnar storage in this blog post https://t.co/rYm87OS99f
4: Learn how our Observability team captures, stores, queries & visualizes the health & performance of our services. https://t.co/J5mDvloYdy
5: We just open sourced @summingbird, streaming mapreduce with @scalding and @stormprocessor https://t.co/iLgkUEiUA7
6: RT @twu: TONIGHT: Don't miss @pandemona talk about the inspiration & development of @vineapp for Android! http://t.co/sn8eZCtkJS
7: Learn about the accessibility improvements our @a11yteam has made for timelines on http://t.co/aPN62RjZeb http://t.co/Hr98DkMPfb
8: RT @SirPatStew: Delighted to have been invited to The Mothership--Twitter HQ. @twoffice Thank you everyone for the hospitality.
9: RT @twitter: Twitter... The final frontier? These are the voyages of @SirPatStew. https://t.co/a8IMWz7f9w
10: An inside (and detailed) look at re-architecting Twitter. Plus, a new Tweets-per-second peak: 143,199 Tweets. https://t.co/LKH4UdScFi
11: Congrats, @GirlsWhoCode grads! It was a pleasure to host such a great, inspiring group of young women this summer. http://t.co/fLTThIn1zp
12: Live near New York City? Join us August 20th for the NYC @ApacheMesos meetup! https://t.co/lfMKVYRpRc
13: RT @twu: To help @TwitterEng continue to develop their skills, we’ll offer technical classes like Android Internals and Python Bootcamp.
14: RT @twu: We want Twitter to be the best place in the world for engineers to work. Announcing Twitter University: https://t.co/q101ZuYAAZ
15: We've been exploring ways to provide greater transparency across our teams. Learn more about our process here: https://t.co/njrKMbomNk
16: "Login verification is more secure and easier to use. And you can still sign in even if you lose your phone." https://t.co/ovc1etOs41
17: On Twitter's move to the Java Virtual Machine: http://t.co/sZd4dFmS8Q "Reliability and performance are huge goals for us" -@rgbenson
18: RT @JoinTheFlock: We've opened an office in Sunnyvale & are hiring for a variety of positions. Here's one of them: https://t.co/KEIyUqDA3n …
19: The @ParquetFormat team released 1.0: Columnar Storage for #Hadoop https://t.co/4BWFXJtGfZ