It has been a while since I’ve done a very technical tutorial on PyThoughts, so I thought I’d write one today. It’s about getting data from Facebook, and the best ways to do that.

1.Using the Graph API

This is by far the easiest way to get data from Facebook, although it might not be a complete solution for you. The method is simple, and I do it like this:

$pageid = 6233046685; // This is the page id, but it could also be the page name. For this instance, I have used the page id for the Ron Paul fan page – however, I could also simply have used “ronpaul” and it would have worked.
// In this next line, I get the file contents of the page, and I translate them using PHP’s json decoder:
$graph = json_decode(file_get_contents("https://graph.facebook.com/".$pageid));

Now I have all of this information about Ron Paul’s fanpage:

“id”: “6233046685”,
“name”: “Ron Paul”,
“picture”: “http://profile.ak.fbcdn.net/hprofile-ak-snc4/41576_6233046685_9552_s.jpg”,
“link”: “http://www.facebook.com/ronpaul”,
“category”: “Politician”,
“username”: “ronpaul”,
“location”: { “city”: “Washington”, “state”: “DC”, “country”: “United States” },
“phone”: “1-800-RON-PAUL”,
“hometown”: “Lake Jackson, TX”,
“affiliation”: “Republican”,
“likes”: 296188

If I want to save or output any of this information specifically, I just need to query my $graph variable, like so:

$politicianname = $graph->name;
$politicianlikes = $graph->likes;
echo  $politicianname.” has “.$politicianlikes.” fans on Facebook!”;

This should result with the output: “Ron Paul has 296188 fans on Facebook!”

This would be an easy way, let’s say, to create an application that compares the popularity of politicians on the Facebook social network. All you would need is the page id or page name of all of the major politicians, and output them into a nice format.

2. Using the REST Server API

This is also a simple method of getting details about an entity on Facebook, comparable to the Graph API. You can use Facebook’s special query language, FQL (similar to all SQL languages), to request this data. This is how I use it:

$pageid = 6233046685;
$fquery = “SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$pageid”;
$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=”.$fquery);
$fancount = $xml->page->fan_count;
echo “Ron Paul has ”.$fancount.” supporters on Facebook!”;

3. Being Totally Ridiculous and Searching the Actual Page

Sometimes requests like this just won’t do, and you need to hack the damn page. For this, you need to load the page up in PHP, and use its source code to retrieve the information that you want.

For instance, say you want to find out the Facebook Page ID using the page URL. There might be a way to do this using the fql, but I couldn’t think of a way in the time that I was planning this tutorial. I decided just to make a dirty hack and have fun with it.

Here I am loading the page:

$pagename = “ronpaul”;
$dirtyurl = "http://www.facebook.com/".$pagename;
$response = file_get_contents($dirtyurl);
if ($response){
$matches = split('pid=', $response);
$closer = split("&",$matches[1]);
$fanpage = $closer[0];
}

What this is doing, is it’s getting the contents of the Facebook page (hopefully), and putting them into this variable called “response”. Then, we’re splitting that content up into matches of “pid=” (Facebook’s way of saying Page ID). We’re then taking the first occurrence of pid (which is hopefully the id of the page that we’re on), and splitting that from the rest of the page, by splitting it at the “&” sign, which Facebook uses to end off the id sequence.

So, in the actually source code on Facebook, it looks like “…pid=6233046685&…”, and we’re getting the number that comes between “pid=” and “&”. So in this way, we can get the page id of any page on Facebook, using just the URL or name of the page!

Join a community of 3000+ users by signing up to our weekly newsletter!
Or catch us on one of these social networks