【发布时间】:2011-07-11 12:31:26
【问题描述】:
我需要检查给定的 Facebook 应用 ID 是否有效。此外,我需要检查为此应用程序 ID 设置了哪些域和站点配置。不管是通过 PHP 还是 Javascript 完成。
我到处检查,但找不到任何有关此的信息。有什么想法吗?
【问题讨论】:
标签: php javascript api facebook
我需要检查给定的 Facebook 应用 ID 是否有效。此外,我需要检查为此应用程序 ID 设置了哪些域和站点配置。不管是通过 PHP 还是 Javascript 完成。
我到处检查,但找不到任何有关此的信息。有什么想法吗?
【问题讨论】:
标签: php javascript api facebook
您可以通过转到http://graph.facebook.com/<APP_ID> 并查看它是否加载了您期望的内容来验证 ID。对于应用信息,请尝试使用admin.getAppProperties,使用属性from this list。
【讨论】:
使用图形 API。只需请求:
https://graph.facebook.com/<appid>
它应该返回一个如下所示的 JSON 对象:
{
id: "<appid>",
name: "<appname>",
category: "<app category>",
subcategory: "<app subcategory>",
link: "<applink>",
type: "application",
}
因此,要验证指定的 app_id 是否确实是应用程序的 id,请查找 type 属性并检查它是否为 application.id。如果根本没有找到 id,它只会返回 false。
更多信息:https://developers.facebook.com/docs/reference/api/application/
例如:
<?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');
}
?>
【讨论】:
使用图形 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 . . .
【讨论】: