here is your working example.
it has a redirect and works entirely in php, i didnt do javascript beacuse this is easier and faster to write.
the main difference is that the returned code after the authorization page is only a code which allows you to fetch the actual access token incombination with your client secret.
otherwise anyone could get an access token for yuor application or you would have to pass your secret token in the url.
thats the reason for the second step.
in javascript we dont need that because facebook only redirects back to a whitelisted domain
and as the access token is in the url fragment after the # tag the server cant access it, only the client. and this is ensured to be yours erver as your domain must be whitelisted.
but it needs more client interaction...
well anyway. you can use the code i wrote for yuo and you can also do it in a popup. you just have to pass your variables to the form or whatever you are doing but this shouldnt be a problem. i did it this way because you said you needed it in php. a good javascript example can be found on the facebook connect page here: http://developers.facebook.com/docs/guides/web#login
any questions? comment!
ps: i put the @ sign before the file_get_contents function because you might geht ssl errors, you should actually use curl for that and do error handling!
save this file as whatever.php to your server, check the 4 config variables in the top and hit it
<?
// your app id
$app_id = "123";
// your secret token
$mysecret = '***';
// your website correctly set up in facebook app config
$redirecturl = "http://www.yourdomain.com/whatever/thisfile.php?action=authorized";
// what you want to fetch
$scopes = array('email','user_interests'); // whatever you want
if($_GET['action'] == 'authorize') {
$url = "https://graph.facebook.com/oauth/authorize?client_id=".$app_id."&redirect_uri=";
$url .= urlencode($redirecturl);
$url .= "&display=popup";
$url .= "&scope=".implode(',',$scopes);
header("Location: $url");
exit();
} else if($_GET['action'] == 'authorized') {
$code = $_GET['code'];
$tokenurl = 'https://graph.facebook.com/oauth/access_token'.
'?client_id='.$app_id .
'&redirect_uri='.urlencode($redirecturl).
'&client_secret='.$mysecret.
'&code='.$code;
$token = @file_get_contents($tokenurl);
$token = preg_match('/access_token=(.*)&/',$token,$extracted_token);
$token = $extracted_token[1];
$dataurl = 'https://graph.facebook.com/me?access_token='.$token;
$result = @file_get_contents($dataurl);
$data = json_decode($result);
echo "<pre>".print_r($data,true)."</pre>";
} else {
?><a href="?action=authorize">click here to immport your data</a><?
}
?>