IvanKristianto

Passionately Share and Learn

  • Home
  • WordPress
    • Meetup
    • Learn WordPress
    • Tips & Tricks
    • Useful Plugins
    • Themes
  • Security
  • Web Development
    • Learn PHP
    • Git
    • Web Design
    • Web Server
    • Web Browser
  • Linux
  • About Me
You are here:
  1. Home
  2. Web Development»Web Programming
  3. Get Last Tweet Without OAuth Using PHP

Get Last Tweet Without OAuth Using PHP

December 18, 2010 • 2 Comments

Get last tweet of your tweets or someone’s tweet with username doesn’t need OAuth. Twitter have their own API to get last tweet with username. You can access the API with http://api.twitter.com/1/statuses/user_timeline/, which some arguments supplied. You can read more detail about this API function here.

I have create a simple function to parse last tweet from a username:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function parse_feed($usernames, $limit=5) {
    $usernames = str_replace("www.", "", $usernames);
    $usernames = str_replace("http://twitter.com/", "", $usernames);
        $username_for_feed = str_replace(" ", "+OR+from%3A", $usernames);
    $feed = "http://api.twitter.com/1/statuses/user_timeline/".$username_for_feed.".atom?callback=?";
    $cache_rss = file_get_contents($feed);
    if (!$cache_rss) {
        // we didn't get anything back from twitter
        echo "<!-- ERROR: Twitter feed was blank! Using cache file. -->";
    }
 
    // clean up and output the twitter feed
    $feed = str_replace("&amp;", "&", $cache_rss);
    $feed = str_replace("&lt;", "<", $feed);
    $feed = str_replace("&gt;", ">", $feed);
    $clean = explode("<entry>", $feed);
    $clean = str_replace("&quot;", "'", $clean);
    $clean = str_replace("&apos;", "'", $clean);
    $amount = count($clean) - 1;
    if($amount > $limit) $amount=$limit;
    $tweets = array();
    if ($amount) { // are there any tweets?
        for ($i = 1; $i <= $amount; $i++) {
            $entry_close = explode("</entry>", $clean[$i]);
            $clean_content_1 = explode("<content type="html">", $entry_close[0]);
            $clean_content = explode("</content>", $clean_content_1[1]);
            $clean_name_2 = explode("<name>", $entry_close[0]);
            $clean_name_1 = explode("(", $clean_name_2[1]);
            $clean_name = explode(")</name>", $clean_name_1[1]);
            $clean_user = explode(" (", $clean_name_2[1]);
            $clean_lower_user = strtolower($clean_user[0]);
            $clean_uri_1 = explode("<uri>", $entry_close[0]);
            $clean_uri = explode("</uri>", $clean_uri_1[1]);
            $clean_time_1 = explode("<published>", $entry_close[0]);
            $clean_time = explode("</published>", $clean_time_1[1]);
            $unix_time = strtotime($clean_time[0]);
            $pretty_time = relativeTime($unix_time);
            $tweets[] = array('content' => $clean_content[0], 'time' => $pretty_time);
        }
    }
    return $tweets;
}

How to use it:

1
print_r(parse_feed('ivankrisdotcom'));

The function will return array of tweets. That’s it, if you like to know more, you can follow me on Twitter or Facebook.

Share this:

  • Facebook
  • Twitter
  • LinkedIn
  • Pocket

Related

Categories: Web Programming Tagged With: downloads, last tweets, load tweets using php, php, read tweets php, read tweets php oauth, read twitter feed without oauth, recent tweets php, retrieve last tweet, retrieve past tweet twittter, retrieve tweets using php application, retrive tweets from twitter, tweet from command line oauth, twitter, twitter api, twitter php api get latest tweets for user

Comments

  1. Anonymous says:
    January 4, 2011 at 11:28 pm

    This is a pretty hacky method, that could result in a lot of issues, particularly with character encodings.

    Just load the atom feed with and then do an xpath query on it to get all the data you want on the last 20 tweets.

    $xml = simplexml_load_file($url);
    foreach ($xml->xpath('entry') as $entry) {
    print $entry->title; //prints the tweet
    }

    Or better yet load the json file version and decode with json_decode to get a nice object).

    Reply
    • Ivan Kristianto says:
      January 9, 2011 at 4:27 pm

      Thank you very much for the tips mate. I'll try it soon.

      Reply

Give me your feedbackCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ivan Kristianto

Ivan Kristianto

I am a Senior Web Engineer at Human Made and Google Developer Expert in Web Technology, Lead organiser of Jakarta WordPress Meetup and WordCamp Jakarta Organiser.

View Full Profile →

Copyright © 2025 · minimalist built on Calibrefx Framework · Powered By WordPress ·

Return To Top