Last active
January 22, 2024 11:42
-
-
Save niraj-shah/fcd17411def017e3aefc to your computer and use it in GitHub Desktop.
Creating a Facebook Page Tab App using the Facebook PHP SDK 4.0.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// required Facebook PHP SDK v4.0.9 or later. | |
// include required files form Facebook SDK | |
require_once( 'Facebook/HttpClients/FacebookHttpable.php' ); | |
require_once( 'Facebook/HttpClients/FacebookCurl.php' ); | |
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' ); | |
require_once( 'Facebook/Entities/AccessToken.php' ); | |
require_once( 'Facebook/Entities/SignedRequest.php' ); | |
require_once( 'Facebook/FacebookSession.php' ); | |
require_once( 'Facebook/FacebookRedirectLoginHelper.php' ); | |
require_once( 'Facebook/FacebookSignedRequestFromInputHelper.php' ); // added in v4.0.9 | |
require_once( 'Facebook/FacebookRequest.php' ); | |
require_once( 'Facebook/FacebookResponse.php' ); | |
require_once( 'Facebook/FacebookSDKException.php' ); | |
require_once( 'Facebook/FacebookRequestException.php' ); | |
require_once( 'Facebook/FacebookOtherException.php' ); | |
require_once( 'Facebook/FacebookAuthorizationException.php' ); | |
// these two classes required for canvas and tab apps | |
require_once( 'Facebook/FacebookCanvasLoginHelper.php' ); | |
require_once( 'Facebook/FacebookPageTabHelper.php' ); | |
require_once( 'Facebook/GraphObject.php' ); | |
require_once( 'Facebook/GraphSessionInfo.php' ); | |
use Facebook\HttpClients\FacebookHttpable; | |
use Facebook\HttpClients\FacebookCurl; | |
use Facebook\HttpClients\FacebookCurlHttpClient; | |
use Facebook\Entities\AccessToken; | |
use Facebook\Entities\SignedRequest; | |
use Facebook\FacebookSession; | |
use Facebook\FacebookRedirectLoginHelper; | |
use Facebook\FacebookSignedRequestFromInputHelper; // added in v4.0.9 | |
use Facebook\FacebookRequest; | |
use Facebook\FacebookResponse; | |
use Facebook\FacebookSDKException; | |
use Facebook\FacebookRequestException; | |
use Facebook\FacebookOtherException; | |
use Facebook\FacebookAuthorizationException; | |
use Facebook\GraphObject; | |
use Facebook\GraphSessionInfo; | |
// these two classes required for canvas and tab apps | |
use Facebook\FacebookCanvasLoginHelper; | |
use Facebook\FacebookPageTabHelper; | |
// start session | |
session_start(); | |
// init app with app id and secret | |
FacebookSession::setDefaultApplication( 'xxx','yyy' ); | |
// init login helper | |
$helper = new FacebookRedirectLoginHelper( 'http://sites.local/php-sdk-4.0/redirect.php' ); | |
// init page tab helper | |
$pageHelper = new FacebookPageTabHelper(); | |
// get session from the page | |
$session = $pageHelper->getSession(); | |
// get page_id | |
echo '<p>You are currently viewing page: '. $pageHelper->getPageId() . '</p>'; | |
// get like status - use for likegates | |
echo '<p>You have '. ( $pageHelper->isLiked() ? 'LIKED' : 'NOT liked' ) . ' this page</p>'; | |
// get admin status | |
echo '<p>You are '. ( $pageHelper->isAdmin() ? 'an ADMIN' : 'NOT an ADMIN' ) . '</p>'; | |
// see if we have a session | |
if ( isset( $session ) ) { | |
// show logged-in user id | |
echo 'User Id: ' . $pageHelper->getUserId(); | |
// graph api request for user data | |
$request = new FacebookRequest( $session, 'GET', '/me' ); | |
$response = $request->execute(); | |
// get response | |
$graphObject = $response->getGraphObject()->asArray(); | |
// print profile data | |
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>'; | |
// print logout url, target = _top to break out of page frame | |
echo '<a href="' . $helper->getLogoutUrl( $session, 'http://sites.local/php-sdk-4.0/redirect.php' ) . '" target="_top">Logout</a>'; | |
} else { | |
// show login url, target = _top to break out of page frame | |
echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '" target="_top">Login</a>'; | |
} |
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /xxx/FacebookRedirectLoginHelper.php on line 191
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your example because facebook sdk documentation REALLY sucks!!