Está en la página 1de 20

PHPFacebookCode

TagFriends
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.5'
]);

$helper=$fb>getRedirectLoginHelper();

//appdirectorycouldbeanythingbutwebsiteURLmustmatchtheURLgiveninthedevelopers.facebook.com/apps
define('APP_URL','http://sohaibilyas.com/fbapp/');

$permissions=['publish_actions','user_friends'];//optional

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();

}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

//gettingshortlivedaccesstoken

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

//settingdefaultaccesstokentobeusedinscript

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

//redirecttheuserbacktothesamepageifithas"code"GETvariable

if(isset($_GET['code'])){

header('Location:./');

//validatinguseraccesstoken

try{

$user=$fb>get('/me');

$user=$user>getGraphNode()>asArray();

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

session_destroy();

//ifaccesstokenisinvalidorexpiredyoucansimplyredirecttologinpageusingheader()function

exit;

}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//gettingallfriendsofuser

$friends=$fb>get('/me/taggable_friends');

$friends=$friends>getGraphEdge()>asArray();

//gettingrandomfriendoutofallfriends

$totalFriends=count($friends);

$random=rand(0,$totalFriends);

//postingonfacebookandtaggingfriendwithit

$post=$fb>post('/me/feed',array('message'=>'mymessage','tags'=>$friends[$random]['id']));

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']
}else{

//replaceyourwebsiteURLsameasaddedinthedevelopers.facebook.com/appse.g.ifyouusedhttpinsteadofhttpsand
youusednonwwwversionorwwwversionofyourwebsitethenyoumustaddthesamehere

$loginUrl=$helper>getLoginUrl(APP_URL,$permissions);

echo'<ahref="'.$loginUrl.'">LoginwithFacebook!</a>';
}

CheckDeclined/GrantedPermissions
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.5',
]);

$helper=$fb>getRedirectLoginHelper();

$permissions=['publish_actions'];

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();


}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

//gettingshortlivedaccesstoken

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

//settingdefaultaccesstokentobeusedinscript

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

//redirecttheuserbacktothesamepageifithas"code"GETvariable

if(isset($_GET['code'])){

header('Location:./');

//gettingdeclinedandgrantedpermissions
$permissions=$fb>get('/me/permissions');

$permissions=$permissions>getGraphEdge()>asArray();

//printingdeclinedandgrantedpermission

echo"<pre>";

print_r($permissions);

echo"</pre>";

//makingnewloginURLwithdeclinedpermissionsattachedtoit

foreach($permissionsas$key){

if($key['status']=='declined'){

$declined[]=$key['permission'];

$loginUrl=$helper>getLoginUrl('http://sohaibilyas.com/APP_DIR/',$declined);

echo'<ahref="'.$loginUrl.'">LoginwithFacebook!</a>';

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']

}else{

//replaceyourwebsiteURLsameasaddedinthedevelopers.facebook.com/appse.g.ifyouusedhttpinsteadofhttpsand
youusednonwwwversionorwwwversionofyourwebsitethenyoumustaddthesamehere

$loginUrl=$helper>getLoginUrl('http://sohaibilyas.com/APP_DIR/',$permissions);

echo'<ahref="'.$loginUrl.'">LoginwithFacebook!</a>';
}

GetBasicPageInfo
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.5',
]);

$helper=$fb>getRedirectLoginHelper();

$permissions=[];//optional

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();

}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

//gettingshortlivedaccesstoken

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

//settingdefaultaccesstokentobeusedinscript

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

//redirecttheuserbacktothesamepageifithas"code"GETvariable

if(isset($_GET['code'])){

header('Location:./');

//gettingbasicinfoaboutuser

try{

$profile_request=$fb>get('/me');

$profile=$profile_request>getGraphNode()>asArray();

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

session_destroy();

//redirectinguserbacktoapploginpage

header("Location:./");

exit;

}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//getbasicpageinfo

$page=$fb>get('/funnydemons?fields=username,picture.width(500),cover,');

$page=$page>getGraphNode()>asArray();

echo"<imgsrc='{$page['cover']['source']}'>";

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']
}else{

//replaceyourwebsiteURLsameasaddedinthedevelopers.facebook.com/appse.g.ifyouusedhttpinsteadofhttpsand
youusednonwwwversionorwwwversionofyourwebsitethenyoumustaddthesamehere

$loginUrl=$helper>getLoginUrl('http://sohaibilyas.com/APP_DIR/',$permissions);

echo'<ahref="'.$loginUrl.'">LoginwithFacebook!</a>';
}

GetLikes,Data,Posts,Photos
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.5'
]);

$helper=$fb>getRedirectLoginHelper();

//appdirectorycouldbeanythingbutwebsiteURLmustmatchtheURLgiveninthedevelopers.facebook.com/apps
define('APP_URL','http://sohaibilyas.com/fbapp/');

$permissions=['user_posts','user_photos'];//optional

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();

}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();


exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

//gettingshortlivedaccesstoken

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

//settingdefaultaccesstokentobeusedinscript

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

//redirecttheuserbacktothesamepageifithas"code"GETvariable

if(isset($_GET['code'])){

header('Location:./');

//validatinguseraccesstoken

try{

$user=$fb>get('/me');

$user=$user>getGraphNode()>asArray();

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

session_destroy();

//ifaccesstokenisinvalidorexpiredyoucansimplyredirecttologinpageusingheader()function

exit;

}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//gettinglikesdataofrecent100postsbyuser

$getPostsLikes=$fb>get('/me/posts?fields=likes.limit(1000){name,id}&limit=100');

$getPostsLikes=$getPostsLikes>getGraphEdge()>asArray();

//printinglikesdataasperrequirements

foreach($getPostsLikesas$key){

if(isset($key['likes'])){

echocount($key['likes']).'<br>';
foreach($key['likes']as$key){

echo$key['name'].'<br>';

//gettinglikesdataofrecent100photosbyuser


$getPhotosLikes=$fb>get('/me/photos?fields=likes.limit(1000){name,id}&limit=100&type=uploaded');

$getPhotosLikes=$getPhotosLikes>getGraphEdge()>asArray();

//printinglikesdataasperrequirements

foreach($getPhotosLikesas$key){

if(isset($key['likes'])){

echocount($key['likes']).'<br>';

foreach($key['likes']as$key){

echo$key['name'].'<br>';

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']
}else{

//replaceyourwebsiteURLsameasaddedinthedevelopers.facebook.com/appse.g.ifyouusedhttpinsteadofhttpsand
youusednonwwwversionorwwwversionofyourwebsitethenyoumustaddthesamehere

$loginUrl=$helper>getLoginUrl(APP_URL,$permissions);

echo'<ahref="'.$loginUrl.'">LoginwithFacebook!</a>';
}

GetListofFriendsNames
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.4',
]);

$helper=$fb>getCanvasHelper();

$permissions=['user_friends'];//optionnal

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();

}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler
$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone
$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

//validatingtheaccesstoken
try{

$request=$fb>get('/me');
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

if($e>getCode()==190){

unset($_SESSION['facebook_access_token']);

$helper=$fb>getRedirectLoginHelper();

$loginUrl=$helper>getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/',$permissions);

echo"<script>window.top.location.href='".$loginUrl."'</script>";

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}
//getlistoffriends'names
try{

$requestFriends=$fb>get('/me/taggable_friends?fields=name&limit=100');

$friends=$requestFriends>getGraphEdge();
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}
//ifhavemorefriendsthan100aswedefinedthelimitaboveonlineno.68
if($fb>next($friends)){

$allFriends=array();

$friendsArray=$friends>asArray();

$allFriends=array_merge($friendsArray,$allFriends);

while($friends=$fb>next($friends)){

$friendsArray=$friends>asArray();

$allFriends=array_merge($friendsArray,$allFriends);

foreach($allFriendsas$key){

echo$key['name']."<br>";

echocount($allfriends);
}else{

$allFriends=$friends>asArray();

$totalFriends=count($allFriends);

foreach($allFriendsas$key){

echo$key['name']."<br>";

}else{

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']
$helper=$fb>getRedirectLoginHelper();
$loginUrl=$helper>getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/',$permissions);
echo"<script>window.top.location.href='".$loginUrl."'</script>";

GetListofLikedPages
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.4',
]);

$helper=$fb>getCanvasHelper();

$permissions=['user_likes'];//optionnal

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();

}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

//validatingtheaccesstoken
try{

$request=$fb>get('/me');
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

if($e>getCode()==190){

unset($_SESSION['facebook_access_token']);

$helper=$fb>getRedirectLoginHelper();

$loginUrl=$helper>getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/',$permissions);

echo"<script>window.top.location.href='".$loginUrl."'</script>";

exit;

}
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}
//getlistofpageslikedbyuser
try{

$requestLikes=$fb>get('/me/likes?limit=100');

$likes=$requestLikes>getGraphEdge();
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}
$totalLikes=array();
if($fb>next($likes)){

$likesArray=$likes>asArray();

$totalLikes=array_merge($totalLikes,$likesArray);

while($likes=$fb>next($likes)){

$likesArray=$likes>asArray();

$totalLikes=array_merge($totalLikes,$likesArray);

}
}else{

$likesArray=$likes>asArray();

$totalLikes=array_merge($totalLikes,$likesArray);
}
//printingdataonscreen
foreach($totalLikesas$key){

echo$key['name'].'<br>';
}
//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']
$helper=$fb>getRedirectLoginHelper();
$loginUrl=$helper>getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/',$permissions);
echo"<script>window.top.location.href='".$loginUrl."'</script>";

10

GetMoreUserInfo
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.5',
]);

$helper=$fb>getRedirectLoginHelper();

$permissions=['user_birthday','user_location','user_website'];//optional

try{

if(isset($_SESSION['localhost_app_token'])){

$accessToken=$_SESSION['localhost_app_token'];

}else{

$accessToken=$helper>getAccessToken();

}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['localhost_app_token'])){

$fb>setDefaultAccessToken($_SESSION['localhost_app_token']);

}else{

//gettingshortlivedaccesstoken

$_SESSION['localhost_app_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['localhost_app_token']);

$_SESSION['localhost_app_token']=(string)$longLivedAccessToken;

//settingdefaultaccesstokentobeusedinscript

$fb>setDefaultAccessToken($_SESSION['localhost_app_token']);

//redirecttheuserbacktothesamepageifithas"code"GETvariable

if(isset($_GET['code'])){

header('Location:./');

//gettingbasicinfoaboutuser
try{

11

$profile_request=$fb>get('/me?fields=name,first_name,last_name,birthday,website,location');

$profile=$profile_request>getGraphNode()>asArray();

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

session_destroy();

//redirectinguserbacktoapploginpage

header("Location:./");

exit;

}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//printing$profilearrayonthescreenwhichholdsthebasicinfoaboutuser

echo$profile['birthday']>format('dmY');

echo$profile['website'];

echo$profile['location']['name'];

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['localhost_app_token']
}else{

//replaceyourwebsiteURLsameasaddedinthedevelopers.facebook.com/appse.g.ifyouusedhttpinsteadofhttpsand
youusednonwwwversionorwwwversionofyourwebsitethenyoumustaddthesamehere

$loginUrl=$helper>getLoginUrl('http://sohaibilyas.com/fbapp/',$permissions);

echo'<ahref="'.$loginUrl.'">LoginwithFacebook!</a>';
}

GetUserProfilePicture
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.4',
]);

$helper=$fb>getCanvasHelper();

$permissions=['email'];//optionnal

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();

}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

12

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

//validatingtheaccesstoken

try{

$request=$fb>get('/me');

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

if($e>getCode()==190){

unset($_SESSION['facebook_access_token']);

$helper=$fb>getRedirectLoginHelper();

$loginUrl=$helper>getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/',$permissions);

echo"<script>window.top.location.href='".$loginUrl."'</script>";

exit;

}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//gettingprofilepictureoftheuser

try{

$requestPicture=$fb>get('/me/picture?redirect=false&height=300');//gettinguserpicture

$requestProfile=$fb>get('/me');//gettingbasicinfo

$picture=$requestPicture>getGraphUser();

$profile=$requestProfile>getGraphUser();

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;

}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//showingpictureonthescreen

echo"<imgsrc='".$picture['url']."'/>";

//savingpicture

$img=__DIR__.'/'.$profile['id'].'.jpg';

file_put_contents($img,file_get_contents($picture['url']));

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']

13

}else{

$helper=$fb>getRedirectLoginHelper();
$loginUrl=$helper>getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/');
echo"<script>window.top.location.href='".$loginUrl."'</script>";

LoginonWebsite,GetBasicInfo
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.4',
]);

$helper=$fb>getRedirectLoginHelper();

$permissions=['email'];//optional

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();

}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

//gettingshortlivedaccesstoken

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

//settingdefaultaccesstokentobeusedinscript

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

14


//redirecttheuserbacktothesamepageifithas"code"GETvariable

if(isset($_GET['code'])){

header('Location:./');

//gettingbasicinfoaboutuser

try{

$profile_request=$fb>get('/me?fields=name,first_name,last_name,email');

$profile=$profile_request>getGraphNode()>asArray();

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

session_destroy();

//redirectinguserbacktoapploginpage

header("Location:./");

exit;

}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//printing$profilearrayonthescreenwhichholdsthebasicinfoaboutuser

print_r($profile);

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']
}else{

//replaceyourwebsiteURLsameasaddedinthedevelopers.facebook.com/appse.g.ifyouusedhttpinsteadofhttpsand
youusednonwwwversionorwwwversionofyourwebsitethenyoumustaddthesamehere

$loginUrl=$helper>getLoginUrl('https://sohaibilyas.com/fbapp/',$permissions);

echo'<ahref="'.$loginUrl.'">LoginwithFacebook!</a>';
}

Search
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.5'
]);

$helper=$fb>getRedirectLoginHelper();

//appdirectorycouldbeanythingbutwebsiteURLmustmatchtheURLgiveninthedevelopers.facebook.com/apps
define('APP_URL','http://sohaibilyas.com/APP_DIR/');

$permissions=[];//optional

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();

15

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

//gettingshortlivedaccesstoken

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

//settingdefaultaccesstokentobeusedinscript

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

//redirecttheuserbacktothesamepageifithas"code"GETvariable

if(isset($_GET['code'])){

header('Location:./');

//validatinguseraccesstoken

try{

$user=$fb>get('/me');

$user=$user>getGraphNode()>asArray();

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

session_destroy();

//ifaccesstokenisinvalidorexpiredyoucansimplyredirecttologinpageusingheader()function

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//typecanbeuser,group,pageorevent

$search=$fb>get('/search?q=programming&type=page');

$search=$search>getGraphEdge()>asArray();

foreach($searchas$key){

echo$key['name'].'<br>';

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']
}else{

//replaceyourwebsiteURLsameasaddedinthedevelopers.facebook.com/appse.g.ifyouusedhttpinsteadofhttpsand
youusednonwwwversionorwwwversionofyourwebsitethenyoumustaddthesamehere

16

$loginUrl=$helper>getLoginUrl(APP_URL,$permissions);
echo'<ahref="'.$loginUrl.'">LoginwithFacebook!</a>';

SendHtmlFormData
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.5',
]);

$helper=$fb>getRedirectLoginHelper();

$permissions=['publish_actions'];//optional

try{

if(isset($_SESSION['localhost_app_token'])){

$accessToken=$_SESSION['localhost_app_token'];

}else{

$accessToken=$helper>getAccessToken();
}

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['localhost_app_token'])){

$fb>setDefaultAccessToken($_SESSION['localhost_app_token']);

}else{

//gettingshortlivedaccesstoken

$_SESSION['localhost_app_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['localhost_app_token']);

$_SESSION['localhost_app_token']=(string)$longLivedAccessToken;

//settingdefaultaccesstokentobeusedinscript

$fb>setDefaultAccessToken($_SESSION['localhost_app_token']);

//redirecttheuserbacktothesamepageifithas"code"GETvariable

if(isset($_GET['code'])){

17

header('Location:./');

//gettingbasicinfoaboutuser

try{

$profile_request=$fb>get('/me?fields=name,first_name,last_name');

$profile=$profile_request>getGraphNode()>asArray();

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

session_destroy();

//redirectinguserbacktoapploginpage

header("Location:./");

exit;

}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

if(isset($_POST['message'])){

$post=$fb>post('/me/feed',array('message'=>$_POST['message']));

$post=$post>getGraphNode()>asArray();

echo$post['id'];

?>

<formaction=""method="POST">

Message:<inputname="message"type="text">

<br>

<inputtype="submit"name="submit"value="Submit">

</form>
<?php

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['localhost_app_token']
}else{

//replaceyourwebsiteURLsameasaddedinthedevelopers.facebook.com/appse.g.ifyouusedhttpinsteadofhttpsand
youusednonwwwversionorwwwversionofyourwebsitethenyoumustaddthesamehere

$loginUrl=$helper>getLoginUrl('http://localhost.com/fbapp/',$permissions);

echo'<ahref="'.$loginUrl.'">LoginwithFacebook!</a>';
}

SendNotificationUser
<?php
session_start();
require_once__DIR__.'/src/Facebook/autoload.php';

$fb=newFacebook\Facebook([
'app_id'=>'APP_ID',
'app_secret'=>'APP_SECRET',
'default_graph_version'=>'v2.5',
]);

$helper=$fb>getCanvasHelper();

$permissions=[];//optionnal

18

try{

if(isset($_SESSION['facebook_access_token'])){

$accessToken=$_SESSION['facebook_access_token'];

}else{

$accessToken=$helper>getAccessToken();

}
}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

exit;
}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;
}

if(isset($accessToken)){

if(isset($_SESSION['facebook_access_token'])){

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

}else{

$_SESSION['facebook_access_token']=(string)$accessToken;

//OAuth2.0clienthandler

$oAuth2Client=$fb>getOAuth2Client();

//Exchangesashortlivedaccesstokenforalonglivedone

$longLivedAccessToken=$oAuth2Client>getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token']=(string)$longLivedAccessToken;

$fb>setDefaultAccessToken($_SESSION['facebook_access_token']);

//validatingtheaccesstoken

try{

$request=$fb>get('/me');

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

if($e>getCode()==190){

unset($_SESSION['facebook_access_token']);

$helper=$fb>getRedirectLoginHelper();

$loginUrl=$helper>getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/',$permissions);

echo"<script>window.top.location.href='".$loginUrl."'</script>";

exit;

}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//gettingbasicinfoaboutuser

try{

$profile_request=$fb>get('/me?fields=name,first_name,last_name,email');

$profile=$profile_request>getGraphNode()>asArray();

}catch(Facebook\Exceptions\FacebookResponseException$e){

//WhenGraphreturnsanerror

echo'Graphreturnedanerror:'.$e>getMessage();

unset($_SESSION['facebook_access_token']);

echo"<script>window.top.location.href='https://apps.facebook.com/APP_NAMESPACE/'</script>";

exit;

19


}catch(Facebook\Exceptions\FacebookSDKException$e){

//Whenvalidationfailsorotherlocalissues

echo'FacebookSDKreturnedanerror:'.$e>getMessage();

exit;

//sendingnotificationtouser

$sendNotif=$fb>post('/'.$profile['id'].'/notifications',array('href'=>'?true=43','template'=>'clickhereformore
information!'),'APP_ACCESS_TOKEN');

//Nowyoucanredirecttoanotherpageandusetheaccesstokenfrom$_SESSION['facebook_access_token']
}else{

$helper=$fb>getRedirectLoginHelper();

$loginUrl=$helper>getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/',$permissions);

echo"<script>window.top.location.href='".$loginUrl."'</script>";
}

20