In some situation you will want to create a facebook session for your app, for example you get the code
query string after user granted your app, now you can construct the url like below,
1
|
https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE
|
And then you need to construct session variables base from the access token, these are functions that might help you,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
//Build your session variables
function generateSessionVars($accessToken)
{
$e = explode('|',$accessToken);
$s = explode('-',$e[1]);
$params = array(
'uid'=>trim($s[1]),
'access_token' => trim($accessToken)
);
$params['sig'] = generateSignature($params,APP_SECRET);
return $params;
}
//Generate session signature base on you App Secret
function generateSignature($params,$secret) {
// work with sorted data
ksort($params);
// generate the base string
$base_string = '';
foreach($params as $key => $value) {
$base_string .= $key . '=' . $value;
}
$base_string .= $secret;
return md5($base_string);
}
|
Usage,
1
2
3
4
5
6
7
|
$accessToken = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE");
//get session variables
$params = generateSessionVars($accessToken);
//SET COOKIE DIRECTLY TO BE USED BY PHP SDK
$facebook->setSession($params);
|
The php-sdk will identify whether the session is set.