PHP API TutorialsPHP TutorialsWeb Development

How to get facebook numeric id of profile or page without any API ?

In this article I will show you how to get facebook profile or page numeric id without using any API provided by facebook. In the previous version of Facebook API, it is very easy to get facebook profile/page id using graph API Explorer. A example is shown below :-

https://graph.facebook.com/<!Your Username –>

Above url will results in some JSON format showing your id and type. But now its no longer exists, you have to use facebook user’s request query API to get numeric id which requires access token to get details.

Before Starting you must allow your facebook account to be searched as shown below

Above option is available on this link https://www.facebook.com/settings?tab=privacy

So how can we get facebook numeric id without using any API. Here is an example to get facebook numeric id using any profile or page url.

 

<?php
function get_fbid_from_facebook_by_devildoxx($url)
{ 
		/*First Condition*/
		if( strpos( $url, 'profile.php' ) !== false ) {
			
			$br = explode( 'profile.php?id=', $url );
			$br = explode( '&', $br[ 1 ] );
			
			$numeric_id = $br[ 0 ];
			
		} 
                /*Second Condition*/
                elseif( strpos( $url, '/pages/' ) !== false ) {
			
			$br = explode( '/', $url );
			$br = explode( '?', $br[ count($br) - 1 ] );
			
			$numeric_id = $br[ 0 ];
			
		} 
                /*Third Condition*/
                else {
			 
			$graphurl = 'http://findmyfbid.com';
			$fields = array(
									'url' => urlencode( $url )
							);

		 
			foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
			rtrim($fields_string, '&');
 
			$ch = curl_init();

			//set the url, number of POST vars, POST data
			curl_setopt($ch,CURLOPT_URL, $graphurl);
			curl_setopt($ch,CURLOPT_POST, count($fields));
			curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE);

			//execute post
			$result = curl_exec($ch);

			//close connection
			curl_close($ch);
			
			$br = explode( '<code>', $result );
			$br = explode( '</code>', $br[ 1 ] );
			$numeric_id = trim( $br[ 0 ] );
			
		}
	 
	
	return $numeric_id;
}
?>

What exactly above function do ?

First you have to call that function

<?php
get_fbid_from_facebook_by_devildoxx('<!--Your URL-->')

?>

Once url passes in function, then there are three conditions that has to be passed by your url.

First condition

If url matches syntax like below

https://www.facebook.com/profile.php?id=<!--Your Id-->

Then first condition will work that will get numeric id by exploding url with “?” as mention in function.

Second Condition

If your url matches

http://www.facebook.com/pages/<!--Username-->

Then second condition will work that will also get id using explode function.

Third Conditon

In third condition, I have used http://findmyfbid.com website to get facebook id.  In this I use PHP CURL and dynamically post url to http://findmyfbid.com and in return I got facebook numeric id without using Facebook API.

 

If you like our article please review it

User Rating: 4.76 ( 7 votes)

Anil Mehra

I am Anil Mehra, a passionate, workaholic and a Full Stack Tech Savvy Programmer with true north towards growth. I have worked on 256 live projects in MNC. I am expertise in the field of Programming, Server Management, SEO, Blogging and SMO...

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button

Adblock Detected

Please turn off Ad blocker to view website