Steve
Steve

Reputation: 8809

Convert any facebook URL into a graph ID

Is there a simple, reliable and potentially future-proof way to extract a graph ID from the API given any facebook URL? By "any facebook URL" I mean the URL for a personal profile, page, group, etc. All these things have various formats so I imagine there must be something in the graph API to definitively convert a facebook URL into an ID, right?

Upvotes: 0

Views: 5302

Answers (2)

phwd
phwd

Reputation: 19995

No, there isn't a way to do this simply within the API. You will need a set of pattern matching to match the various types of urls to extract either the id (album,note,photo,status) or username.

For example

Photos

Posts

Pages

  • facebook.com/pages/Joel-Spolsky/106083232757300
  • graph.facebook.com/106083232757300

Videos

  • facebook.com/video/video.php?v=10150398154330484
  • graph.facebook.com/0150398154330484

Events

  • facebook.com/events/138745136241434/
  • etc ...

Then it gets further complicated that even if you were able to get a silver bullet function that handles all these links your app would need to grant access to numerous permissions in order to access certain objects.

You may be able to get away with most links that have the id at the end but not all. So you can maybe use a regular expression catching links that end in numeric characters.

Upvotes: 1

Art
Art

Reputation: 5924

There is! You are looking for the object_url table, which you can query using FQL or directly with a request. See here: http://developers.facebook.com/docs/reference/fql/object_url/

EDIT:

You can also do this, but obviously less optimal:

function getObjectByUrl(url, cb) {
     FB.api("/" + url.replace(/^.*?www.facebook.com/, ""), cb);
}

Upvotes: 0

Related Questions