Skip to content

Getting and parsing Twitter search results in PHP

February 26, 2011

This simple snippet uses PHP’s DOM object to get the Twitter search results for your chosen search term, parse them and then loop through them.

$doc = new DOMDocument();
$doc->load('http://search.twitter.com/search.atom?lang=en&q=%22'.urlencode($yoursearchterm).'%22');

foreach ($doc->getElementsByTagName('entry') as $node) {
	$tweet = trim(strip_tags(html_entity_decode($node->getElementsByTagName('title')->item(0)->nodeValue)));
	$tweetdate = trim(strip_tags(html_entity_decode($node->getElementsByTagName('published')->item(0)->nodeValue)));
	$authornode = $node->getElementsByTagName('author')->item(0);
	$tweetuser = trim(strip_tags(html_entity_decode($nodeauthor->getElementsByTagName('name')->item(0)->nodeValue)));
	$tweetuserurl = trim(strip_tags(html_entity_decode($nodeauthor->getElementsByTagName('uri')->item(0)->nodeValue)));

	// Do something with the tweet here

}
Advertisements

From → Programming

One Comment
  1. Thanks , I’ve recently been searching for information approximately this topic for
    ages and yours is the greatest I’ve came upon so far.
    However, what concerning the bottom line? Are you positive concerning the source?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: