I am building a Facebook application which should not be viewable to anyone under the age of 18, as it makes quite extensive use of the Graph API I wonder is it possible to automatically find the age using that.
Looking at the documentation I can see that it should be possible to find a users age-range using signed requests. However I've not managed to find any actual examples of how to implement this.
Ideally I'd like to be able to end up with a variable which had either TRUE or FALSE as it's value. True being over 18, false being the user is under-age.
I contemplated asking for birthday permissions and calculating the age using that however it seems that many of my friends do not show their birth year so I assume many others do the same, I'm guessing that if the birth year is hidden it will not be accessibly to the Graph API.
Could anyone help me with this?
Thank you.
You could just restrict the entire app programmatically so the app and all its content is only visible to users that meet your restrictions, there's an API for that: https://developers.facebook.com/docs/reference/api/application/#restrictions
There's an example of how to do this here: https://developers.facebook.com/blog/post/574/
Failing that, if your app is running as a page tab or on canvas (i.e apps.facebook.com/something ) the signed_request sent to your app has an approximate age range included
Every user has to indicate their date of birth in order to sign up to the Facebook (since dob field is required). Therefore you can access users birth of date even if they don't prefer to show their age info on their profiles and then calculate their ages once your app got the permission from the user.
I personally prefer signed_request - very easy, public and requires no tokens:
https://developers.facebook.com/docs/reference/login/signed-request/
<?php
$min_age = 0;
if ($_REQUEST) {
$signed_request = $_REQUEST['signed_request'];
$parsed = parse_signed_request($signed_request);
$min_age = (integer)$parsed['user']['age']['min'];
} else {
$min_age = -1;
}
echo $min_age;
if ($min_age >= 18 ) {
}
else {
}
function parse_signed_request($signed_request) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
?>