View Full Version : Problem Displaying Twitter in Text Format

07-27-2012, 11:08 PM
Hi all,
I found some code on a website, which can be used to call the most recent tweet from a twitter RSS feed for a specific user. The first time I executed the document from my live server, the tweet showed up fine, however upon visiting the page a second time, I receive this error message:

Warning: DOMDocument::load(http://twitter.com/statuses/user_timeline/MaxPST.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/maximump/public_html/collegerecruiting.php on line 395

The PHP code I am using currently is this:

$doc = new DOMDocument();

# load the RSS document, edit this line to include your username or user id
if($doc->load('http://twitter.com/statuses/user_timeline/MaxPST.rss')) {

# specify the number of tweets to display, max is 20
$max_tweets = 1;

$i = 1;
foreach ($doc->getElementsByTagName('item') as $node) {
# fetch the title from the RSS feed.
# Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)
$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;

# the title of each tweet starts with "username: " which I want to remove
$tweet = substr($tweet, stripos($tweet, ':') + 1);

# OPTIONAL: turn URLs into links
$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a class="header1" href="$1">$1</a>', $tweet);

echo $tweet;

if ($i++ >= $max_tweets)
echo "</ul>\n";

Any suggestions to how I can rectify the bad request error message, or if you have seen another post with a similar issue that I can review code from, I would appreciate it!