最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to validate Facebook App ID - Stack Overflow

programmeradmin6浏览0评论

I need to check if the given Facebook app id is valid. Also, I need to check which domain and site configurations are set for this app id. It doesn't matter if it's done through PHP or Javascript.

I checked everywhere but couldn't find any information about this. Any ideas?

I need to check if the given Facebook app id is valid. Also, I need to check which domain and site configurations are set for this app id. It doesn't matter if it's done through PHP or Javascript.

I checked everywhere but couldn't find any information about this. Any ideas?

Share Improve this question asked Jul 11, 2011 at 12:31 Serkan YersenSerkan Yersen 1,3133 gold badges10 silver badges20 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

You can validate the ID by going to http://graph.facebook.com/<APP_ID> and seeing if it loads what you expect. For the app information, try using admin.getAppProperties, using properties from this list.

Use the Graph API. Simply request:

https://graph.facebook.com/<appid>

It should return you a JSON object that looks like this:

{
  id: "<appid>",
  name: "<appname>",
  category: "<app category>",
  subcategory: "<app subcategory>",
  link: "<applink>",
  type: "application",
}

So, to validate if the specified app_id is indeed the id of an application, look for the type property and check if it says application. If the id is not found at all, it will just return false.

More info: https://developers.facebook.com/docs/reference/api/application/

For example:

<?php
$app_id = 246554168145;
$object = json_decode(file_get_contents('https://graph.facebook.com/'.$app_id));
// the object is supposed to have a type property (according to the FB docs)
// but doesn't, so checking on the link as well. If that gets fixed
// then check on isset($object->type) && $object->type == 'application'
if ($object && isset($object->link) && strstr($object->link, 'http://www.facebook.com/apps/application.php')) {
   print "The name of this app is: {$object->name}";
} else {
   throw new InvalidArgumentException('This is not the id of an application');
}
?>

Use the Graph API:

$fb = new Facebook\Facebook(/* . . . */);

// Send the request to Graph
try {
  $response = $fb->get('/me');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

var_dump($response);
// class Facebook\FacebookResponse . . .

More info:FacebookResponse for the Facebook SDK for PHP

发布评论

评论列表(0)

  1. 暂无评论