Adam Green
Twitter API Consultant
adam@140dev.com
781-879-2960
@140dev

Free Source Code – Twitter Display: get_tweet_list.php

This script is the real workhorse of the Twitter display plugin. It gets the most recent tweets from the tweets table, assembles each of them into HTML with the tweet_template.txt file, and returns all of them as a single stream of HTML.

There are two ways to call this script. It is called by twitter_display.php with require_once(), to which it responds with a return of the tweet list HTML. It is also called by site.js with an Ajax request when the More Tweets button is clicked, to which it responds by printing the result. Site.js gets the tweet_id from the last tweet in the list and adds it to the request with the query string argument of ?last=[tweet_id]. This is used to find older tweets with smaller ids.

1
2
3
4
5
6
7
8
9
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php 
/**
* get_tweet_list.php
* Return a list of the most recent tweets as HTML
* Older tweets are requested with the query of last=[tweet_id] by site.js
* 
* @author Adam Green <140dev@gmail.com>
* @license GNU Public License
* @version BETA 0.30
*/

require_once('twitter_display_config.php' );
require_once('display_lib.php');
require_once('../../db/db_lib.php' ); 
$oDB = new db;

$query = 'SELECT profile_image_url, created_at, screen_name, 
  name, tweet_text, tweet_id
  FROM tweets ';

// Query string of last=[tweet_id] means that this script was called by site.js
// when the More Tweets button was clicked
if (isset($_GET['last'])) {  
  $query .= 'WHERE tweet_id < "' . $_GET['last'] . '" ';
}

$query .= 'ORDER BY tweet_id DESC LIMIT ' . TWEET_DISPLAY_COUNT;
$result = $oDB->select($query);

// Use the text file tweet_template.txt to construct each tweet in the list
$tweet_template = file_get_contents('tweet_template.txt');
$tweet_list = '';
$tweets_found = 0;
while (($row = mysqli_fetch_assoc($result))
  &&($tweets_found < TWEET_DISPLAY_COUNT)) { 
  
  ++$tweets_found; 
	
  // create a fresh copy of the empty template
  $current_tweet = $tweet_template;
	
  // Fill in the template with the current tweet
  $current_tweet = str_replace( '[profile_image_url]', 
    $row['profile_image_url'], $current_tweet);
  $current_tweet = str_replace( '[created_at]', 
    twitter_time($row['created_at']), $current_tweet);    		
  $current_tweet = str_replace( '[screen_name]', 
	  $row['screen_name'], $current_tweet);  
  $current_tweet = str_replace( '[name]', 
    $row['name'], $current_tweet);    
  $current_tweet = str_replace( '[user_mention_title]', 
    USER_MENTION_TITLE . ' ' . $row['screen_name'] . ' (' . $row['name'] . ')', 
    $current_tweet);  
  $current_tweet = str_replace( '[tweet_display_title]', 
    TWEET_DISPLAY_TITLE, $current_tweet);  
  $current_tweet = str_replace( '[tweet_text]', 
    linkify($row['tweet_text']), $current_tweet);  
		
  // Include each tweet's id so site.js can request older or newer tweets
  $current_tweet = str_replace( '[tweet_id]', 
    $row['tweet_id'], $current_tweet); 
		
  // Add this tweet to the list
  $tweet_list .= $current_tweet;
}

if (!$tweets_found) {
  if (isset($_GET['last'])) {
    $tweet_list = '<strong>No more tweets found</strong><br />';
  } else {
    $tweet_list = '<strong>No tweets found</strong><br />';	
  }	
}

if (isset($_GET['last'])) {
  // Called by site.js with Ajax, so print HTML to the browser
  print $tweet_list;
} else {
  // Called by twitter_display.php with require(), so return the value
  return $tweet_list;
}

?>

streaming_framework