Simple Gnip example

June 7th, 2009 | Categories: PHP

If you’re unfamiliar with Gnip (http://gnip.com/) pronounced “Guh-nip” it’s a content distribution solution for multiple publishers such as twitter, digg, flickr, tumblr, and many more. Rather than polling the service, Gnip allows you to set up a “push” type system where you let a callback URL and when there are new activities or notifications gnip will notify you through the URL provided. What I plan to show you here is a quick bit of code that allows you to quickly retrieve activities from filters using the Gnip PHP Convenience Library. I’m not going to go into a huge amount of depth and I also assume that you know basic programming/PHP paradigms

OK lets get started, what you’ll want to do first is to create a simple filter, I’ll be using the twitter-search publisher for this. So lets say you wanted to get all of the tweets that have the keywords “@mikeluby”, “mike luby”, and “mikeluby.com”. You would start by creating the filter, lets name it “aboutMikeLuby” next you’ll want to put the URL to your callback (lets use http://example.com/gnip_input.php). Finally at the bottom you’ll enter the keywords into the keyword input. This input is comma separated and will simulate an OR conditionals to the twitter search api. Now there is away to create these filters via code, I’ll get into this at a later date. When the activities are returned they are grouped into buckets, there is a new bucket every minute.

At this point you’ll want to make sure you have the Gnip PHP Convenience Library downloaded. (Get it here) In gnip_input.php (the callback when gnip “pushes” data to your service) you’ll have the following:


//This file is called every time Gnip has activities to publish to the current bucket.

include_once( "Gnip.php" );

$time = time(); //Get current time

$gnip = new Services_Gnip( $gnip_username, $gnip_password ); //Create gnip Object

$filter = $gnip->getFilter( "twitter-search", "aboutMikeLuby" ); //Create filter object

$results = $gnip->getFilterActivities( "twitter-search", $filter, $time ); //This returns a Services_Gnip Object of the activites in the specified bucket.

print_r( $results );

There you go, it’s pretty straight forward. Don’t forget to check out Gnip’s documentation.

Comments

TOP