Published on Oct 16, 2010

Facebook Status Search using Graph API – Find What People are Sharing

Facebook Status Search

Facebook Status Search

The rapid evolution of social networks like facebook has helped us to create a vast information store where the users themselves are sharing information and news throughout the globe. So it might not be a bad idea to be able to search through the status updates and shares by the facebook users.

Facebook Graph API makes it very easy to do this with simple API calls and parsing the returned json. For each search query to facebook graph, we need an access_token. I’ve already discussed on how to get an access_token in my post titled People Search using Facebook Graph API – Find Users through Name or Email. So now we can use the following code segment for searching through the status updates and user shares on facebook. We assume that the search query is sent to this page in “search” parameter using get method.

function callFb($url)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
));

$result = curl_exec($ch);
curl_close($ch);
return $result;
}

$url = "https://graph.facebook.com/oauth/access_token?client_id=...&client_secret=...&redirect_uri=...&code=...";
$access_token = callFb($url);
$access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token));

$url = "https://graph.facebook.com/search?access_token=$access_token&q=".urlencode($_GET['search'])."&type=post";
$ret_json = callFb($url);

$status_list = json_decode($ret_json, true);

print_r($status_list);

If you want to show the time of each status or shares done by users like the way facebook do it (showing timestamps as 2 minutes ago, 1 week ago etc.) then you might like to use the following code segment:

//$datefrom is the timestamp for the content , and you can leave the $dateto value to see the current delay

function TimeAgo($datefrom,$dateto=-1)
{
// Defaults and assume if 0 is passed in that
// its an error rather than the epoch

if($datefrom<=0) { return "A long time ago"; }
if($dateto==-1) { $dateto = time(); }

// Calculate the difference in seconds betweeen
// the two timestamps

$difference = $dateto - $datefrom;

// If difference is less than 60 seconds,
// seconds is a good interval of choice

if($difference < 60)
{
$interval = "s";
}

// If difference is between 60 seconds and
// 60 minutes, minutes is a good interval
elseif($difference >= 60 && $difference<60*60)
{
$interval = "n";
}

// If difference is between 1 hour and 24 hours
// hours is a good interval
elseif($difference >= 60*60 && $difference<60*60*24)
{
$interval = "h";
}

// If difference is between 1 day and 7 days
// days is a good interval
elseif($difference >= 60*60*24 && $difference<60*60*24*7)
{
$interval = "d";
}

// If difference is between 1 week and 30 days
// weeks is a good interval
elseif($difference >= 60*60*24*7 && $difference <
60*60*24*30)
{
$interval = "ww";
}

// If difference is between 30 days and 365 days
// months is a good interval, again, the same thing
// applies, if the 29th February happens to exist
// between your 2 dates, the function will return
// the 'incorrect' value for a day
elseif($difference >= 60*60*24*30 && $difference <
60*60*24*365)
{
$interval = "m";
}

// If difference is greater than or equal to 365
// days, return year. This will be incorrect if
// for example, you call the function on the 28th April
// 2008 passing in 29th April 2007. It will return
// 1 year ago when in actual fact (yawn!) not quite
// a year has gone by
elseif($difference >= 60*60*24*365)
{
$interval = "y";
}

// Based on the interval, determine the
// number of units between the two dates
// From this point on, you would be hard
// pushed telling the difference between
// this function and DateDiff. If the $datediff
// returned is 1, be sure to return the singular
// of the unit, e.g. 'day' rather 'days'

switch($interval)
{
case "m":
$months_difference = floor($difference / 60 / 60 / 24 /
29);
while (mktime(date("H", $datefrom), date("i", $datefrom),
date("s", $datefrom), date("n", $datefrom)+($months_difference),
date("j", $dateto), date("Y", $datefrom)) < $dateto)
{
$months_difference++;
}
$datediff = $months_difference;

// We need this in here because it is possible
// to have an 'm' interval and a months
// difference of 12 because we are using 29 days
// in a month

if($datediff==12)
{
$datediff--;
}

$res = ($datediff==1) ? "$datediff month ago" : "$datediff
months ago"
;
break;

case "y":
$datediff = floor($difference / 60 / 60 / 24 / 365);
$res = ($datediff==1) ? "$datediff year ago" : "$datediff
years ago"
;
break;

case "d":
$datediff = floor($difference / 60 / 60 / 24);
$res = ($datediff==1) ? "$datediff day ago" : "$datediff
days ago"
;
break;

case "ww":
$datediff = floor($difference / 60 / 60 / 24 / 7);
$res = ($datediff==1) ? "$datediff week ago" : "$datediff
weeks ago"
;
break;

case "h":
$datediff = floor($difference / 60 / 60);
$res = ($datediff==1) ? "$datediff hour ago" : "$datediff
hours ago"
;
break;

case "n":
$datediff = floor($difference / 60);
$res = ($datediff==1) ? "$datediff minute ago" :
"$datediff minutes ago";
break;

case "s":
$datediff = $difference;
$res = ($datediff==1) ? "$datediff second ago" :
"$datediff seconds ago";
break;
}
return $res;
}

 

6 Comments + Add Comment

  • Siim

    January 2, 2011 at 8:00 am

    Thanks for a nice post.
    Are you aware if it is possible to filter search results also by location. Something like using ‘near’ parameter in twitter.

    • Masum

      January 8, 2011 at 4:37 am

      Hi,
      I also searched for such a feature in the graph API, but didn’t find anything like a location based searching for status updates and user shares. Facebook Graph supports location based search in places where you checkin but not for status updates and user shares.

      You may want to do an FQL query with user address or so, but problem is in that case you need to know a set of user id’s and then search for those users status updates which is not gonna be a global status search. Please let me know if you can find any work around. Thanks :)

  • Susan Fillipow

    April 10, 2011 at 8:34 pm

    i really didnt appreciate finding this site under my name

  • Ahsanul Karim

    April 25, 2011 at 5:42 pm

    I never heard of it. But I learn from your site. Its so gelpful content

  • Giang Le

    July 18, 2011 at 7:07 am

    like

  • Henrique

    September 1, 2012 at 10:31 am

    Hello, great tutorial.
    The code of the demo is available for download?
    I’m trying to implement and I can not.

    Thank you.

Leave a comment

Sponsored