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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| // [fonctions]
function getToken(){
$content = file('token.txt');
return $content[0];
}
function curl_get_file_contents($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: graph.facebook.com'));
$json = curl_exec($ch);
curl_close($ch);
return $json;
}
function getError() {
$json = json_decode(curl_get_file_contents('https://graph.facebook.com/'.$app_id.'/feed?access_token='.getToken()));
if ($json->error) {
if ($json->error->type== "OAuthException") {
return false;
}else {
return true;
}
}
}
// [/fonctions]
// [token] permet de récupérer le token valable 60 jours
$tmp_token = getToken();
if((empty($tmp_token) && !preg_match('/code=/', $_SERVER['REQUEST_URI'])) || (!getError($app_id))) {
$code = 'https://www.facebook.com/dialog/oauth?client_id='.
$app_id."&redirect_uri=".urlencode($callback_url).
'&scope=user_groups&state=lijecreative';
echo("<script> top.location.href='".$code."'</script>");
}
if((empty($tmp_token) && preg_match('/code=/', $_SERVER['REQUEST_URI'])) || (!getError($app_id))) {
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id='.
$app_id."&redirect_uri=".urlencode($callback_url).
'&client_secret='.$app_secret.'&code='.$_REQUEST["code"];
$data = curl_get_file_contents($token_url);
parse_str($data, $params);
$token_60j = $params['access_token'];
$fp = fopen('token.txt','w');
fwrite($fp, $token_60j);
fclose($fp);
}
// [/token] |
Partager