RCNEIL

  • About
  • Work
  • Blog
  • Contact

A Simple How To Get Twitter Statuses With PHP

March 12, 2013

A while back, Twitter deprecated their 1.0 API and the commonly used Javascript method of getting Tweets. Now, they require a lot of nonsense including (if you write in PHP) a PHP library, an APP key, and a whole lot of know-how.

I don’t have, nor want, that know-how.

So, if you’re like me and want something simple to just call a few Tweets and style them how you want, I resorted to a primitive method.

What is the simplest form of transferring information on the web today? RSS. Bingo. So really, if you want a non-styled, very simple feed of your Tweets, call your RSS feed :

https://api.twitter.com/1/statuses/user_timeline/<span style="font-weight: bold;">YOURTWITTERID</span>?count=5

And here’s where the PHP comes in:

<?php 
	$feed = 'https://api.twitter.com/1/statuses/user_timeline/YOURTWITTERID?count=5';
	$tweets = file_get_contents($feed);
	$tweets = str_replace("&", "&", $tweets);	
	$tweets = str_replace("<", "<", $tweets);
	$tweets = str_replace(">", ">", $tweets);
	$tweet = explode("<item>", $tweets);
	$tcount = count($tweet) - 1;
	for ($i = 1; $i <= $tcount; $i++) {
		$endtweet = explode("</item>", $tweet[$i]);
		$title = explode("<title>", $endtweet[0]);
		$content = explode("</title>", $title[1]);		
		
		$content[0] = str_replace("–", "&mdash;", $content[0]);
	
		$content[0] = preg_replace("/(http://|(www.))(([^s<]{4,68})[^s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $content[0]);
		$content[0] = str_replace("$username: ", "", $content[0]);
		$content[0] = preg_replace("/@(w+)/", "<a href="http://www.twitter.com/1" target="_blank">@1</a>", $content[0]);
		$content[0] = preg_replace("/#(w+)/", "<a href="http://search.twitter.com/search?q=1" target="_blank">#1</a>", $content[0]);
		
		$mytweets[] = $content[0];
	}
	while (list(, $v) = each($mytweets)) {
		$tweetout .= "<p>$v</p>";
	}
	$pattern="/'/";
	$replacement="&acute;";
	$tweetout=preg_replace($pattern, $replacement, $tweetout);
		echo $tweetout;
?>

What’s happening is we’re grabbing the RAW feed of our RSS feed, stripping out the XML tags, using preg_replace() to swap out some of the common symbols, and then listing each Tweet through an array.

This is about as plain as it gets, and from here you can go on to style the Tweets as you see fit. Feel free to change your count or functions to modify the raw feed to benefit your needs as well.

Hope that helps!

  • css
  • custom css tweets
  • custom css twitter statuses
  • custom twitter stream
  • get tweets since 1.0 deprecated
  • get tweets with php
  • new twitter api
  • php
  • php twitter feed
  • php twitter statuses
  • php twitter stream
  • simple twitter feed
  • style tweets
  • style twitter feed
  • twitter
  • twitter with php

Leave A Comment

Your email address will not be published. Required fields are marked *

  RCNeil

  • @htmleverything Always pure CSS. But I can tell I am one of the few., Jan 26
  • About
  • Work
  • Blog
  • Contact

All Rights Reserved © 2023 RCNeil Development, LLC