Finding Your Facebook ID

FEw months back someone sent me email asking me to write tutorial to get Facebook ID of any user using their profile URL. I don’t know why he wanted the script so bad, but he will surely be amazed how dead simple this is.

Find You Facebook ID & More

I have been playing with Facebook graph urls and I came up with a decent example below, Just enter Facebook profile URL of any user to get fetch their ID and more.

PHP Tutorial

There are few Facebook user information (id, name, gender,picture etc.) that do not require any permission or SDKs, we can simply point URL to graph.facebook.com and fetch these data.
Here’s a simple Facebook Graph URL that shows my ID and Name :
https://graph.facebook.com/sarancham?fields=id,name
So, simply use PHP cURL to fetch these datas and use them in our scripts :
 
1
2
3
4
5
6
7
8
9
10
11
12
13
$result = get_data('https://graph.facebook.com/sarancham?fields=id,name');
echo 'Hi '.$result->name.', Your Facebook ID is : '.$result->id;

function get_data($json_url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $json_url);
$json_data =  curl_exec($ch);
return json_decode($json_data);
curl_close($ch);
}
If you need complete PHP example, you can download it from URL below. The sample file doesn’t contain jQuery ajax part as shown in the demo above, which I had added later in this page.
DOWNLOAD

No comments:

Post a Comment