Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Graph API - Get ID from Facebook Page URL

I have seen this question but what I want is different.

I want to get the Facebook ID not from a general URL (and therefore conditional if it has Like button or not). I want to get the Facebook ID given a Facebook page using the Graph API.

Notice that Facebook pages can have several formats, such as:

http://www.facebook.com/my_page_name http://www.facebook.com/pages/my_page_name http://www.facebook.com/my_page_ID 

I know I could do some regex to get either the my_page name or my_page_ID, but I am wondering if any one know if GraphAPI is supporting what I want.

like image 322
Hommer Smith Avatar asked Apr 13 '12 22:04

Hommer Smith


People also ask

How do I get Facebook Page Insights on graph API?

You can access Page Insights by typing /insights after the name of your page in the URL field. This command will retrieve all of the Insights associated with your Page. Type "/insights" after your company name. Click the Submit button.


2 Answers

It seems to me that the easiest solution to what you describe is to just get the id/name from the url you have using lastIndexOf("/") (which most languages have an equivalent for) and then get "https://graph.facebook.com/" + id.

The data that this url returns has the id (i.e.: 6708787004) and the username (i.e.: southpark), so regardless of which identifier you use (what you extract from the url using lastIndexOf), you should get the same result.


Edit

This code:

identifier = url.substring(url.lastIndexOf("/")) graphUrl = "https://graph.facebook.com/" + identifier urlJsonData = getGraphData(graphUrl) 

Should work the same (that is result with the same data) for both:

url = http://www.facebook.com/southpark 

And

url = http://www.facebook.com/6708787004 

(you'll obviously need to implement the getGraphData method).

Also, the 2nd url form in the question is not a valid url for pages, at least not from my tests, I get:

You may have clicked an expired link or mistyped the address. Some web addresses are case sensitive.

like image 158
Nitzan Tomer Avatar answered Sep 23 '22 23:09

Nitzan Tomer


The answer to the question is posted above but the method shown below works fine we do not have to perform the regex on the facebook page urls

I got the answer by this method

FB.api('/any_fb_page_url', function(response){   console.log(response); });  

any_fb_page_url can be any of the following types

https://www.facebook.com/my_page_name https://www.facebook.com/pages/my_page_name https://www.facebook.com/my_page_ID 

This are also listed in question above

This code is tested on JS console available on Facebook Developers site tools

like image 44
Suraj Palwe Avatar answered Sep 26 '22 23:09

Suraj Palwe