Get Your Most Recent Twitter Posts Using PHP with RSS (part 3)
Taking the Twitter feed to the next step: pulling from multiple user accounts all at once.
In Part One, we pull a single user’s latest tweet. Then, in Part Two we pull multiple tweets from a single user. Where do we go next? Easy: pull multiple tweets from multiple Twitter users!
Now, if you are here interested in pulling multiple tweets from only your account, I highly suggest you return to Part Two. You will most likely see a better outcome with that script.
However, if you are looking to pull multiple tweets from multiple accounts and display them in reverse chronological order, this is surely the script for you. With the code below, you are able to define what you want before the Twitter block, after the Twitter block, before and after each individual tweet, and after the displayed user name. You can also turn off the displayed user names if you are looking to show all of your own personal tweets from multiple accounts! So, lets get started with the code:
<?php
/* Copyright 2010 Ali Han
http://www.alihan.com.tr/blog/get-your-most-recent-twitter-posts-using-php-with-rss.html
http://www.apache.org/licenses/LICENSE-2.0 */
// Pull from which accounts? Separated by a space, for example: Username Username Username
$usernames = "Username Username Username";
// Number of tweets to pull in, total.
$limit = "5";
// Show username? 0 = No, 1 = Yes.
$show = 1;
// REMEBER: When using HTML, escape double-quotations like this: \"
// This comes before the entire block of tweets.
$prefix = "";
// This comes before each tweet on the feed.
$prefix_sub = "";
// This comes after the username but before the tweet content.
$wedge = "";
// This comes after each tweet on the feed.
$suffix_sub = "";
// This comes after the entire block of tweets.
$suffix = "";
// It is recommended that you do not modify below this point without PHP knowledge.
function parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub) {
$usernames = str_replace(" ", "+OR+from%3A", $usernames);
$feed = "http://search.twitter.com/search.atom?q=from%3A" . $usernames . "&rpp=" . $limit;
$feed = file_get_contents($feed);
$feed = str_replace("&", "&", $feed);
$feed = str_replace("<", "<", $feed);
$feed = str_replace(">", ">", $feed);
$clean = explode("<entry>", $feed);
$amount = count($clean) - 1;
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_uri_1 = explode("<uri>", $entry_close[0]);
$clean_uri = explode("</uri>", $clean_uri_1[1]);
echo $prefix_sub;
if ($show == 1) { echo "<a href=\"" . $clean_uri[0] . "\">" . $clean_name[0] . "</a>" . $wedge; }
echo $clean_content[0];
echo $suffix_sub;
}
}
echo $prefix;
parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub);
echo $suffix;
?>
If you notice, the upper half of the script contains a few variables (each with a description of what each one does) that will modify how your Twitter block is displayed. Here are the eight variables that can be changed: $usernames, $limit, $show, $prefix, $prefix_sub, $wedge, $suffix_sub, $suffix. Let’s go over what each one does:
- $usernames – Which username’s the script will pull tweets from, separated by spaces. (” “)
- $limit – How many tweets for the script to pull. (Twitter set the maximum to 100 tweets.)
- $show – Whether or not to display the user names with the tweets.
- $prefix – What’s to be displayed before the block of tweets.
- $prefix_sub – What’s to be displayed before each individual tweet.
- $wedge – What’s to be displayed after each user name. (If $show is set to 1.)
- $suffix_sub – What’s to be displayed after each individual tweet.
- $suffix – What’s to be displayed after the block of tweets.
What power do these variables hold? A ton. Let’s show an example:
$usernames = "engadget mashable smashingmag"; $limit = "5"; $show = 1; $prefix = "<h1>Twitter Feed</h1><ul>"; $prefix_sub = "<li>"; $wedge = " says: "; $suffix_sub = "</li>"; $suffix = "</ul><p><a href=\"#\">More from our Twitter feeds!</a></p>";
Using the above code on a page without any CSS applied will give you this live working example (I will add soon).
Script not pulling entries? It is possible you are trying to use too many usernames or your usernames that you are using are too long. How is this possible? Twitter has set a 140 character limit to the query that can be applied to a search in the RSS. You can find more information in the Twitter API documentation.
Was this tutorial a little too strong for you? Try refreshing with Part One or Part Two of the Latest Twitter Update With PHP/RSS tutorial!
LICENSE
Copyright 2011 Ali Han
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
( Ali Han, URI:alihan.com.tr/blog )
Get Your Most Recent Twitter Posts Using PHP with RSS (part 3) WordPress Theme
Reviewed by Ali Han on May 18th 2011
Rating:


