Created
December 17, 2012 12:13
-
-
Save arisetyo/4317866 to your computer and use it in GitHub Desktop.
example of using the Facebook PHP SDK
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 | |
require '../facebook-php-sdk/src/facebook.php'; | |
$facebook = new Facebook(array( | |
'appId' => 'YOUR_APP_ID', | |
'secret' => 'YOUR_APP_SECRET', | |
)); | |
$redirecturl = "REDIRECT_URL_AFTER_LOGIN_(THIS_FILE_FOR_THIS_EXAMPLE)"; | |
//GET THE USER DATA | |
$user = $facebook->getUser(); | |
//ADD THE SCOPE publish_stream SO WE CAN WRITE ON WALLS | |
$loginUrl = $facebook->getLoginUrl( array('scope'=>'email,publish_stream', 'redirect_uri' => $redirecturl) ); | |
//CHECK IS USER IS LOGGED IN | |
if($user){ | |
try { | |
$user_profile = $facebook->api('/me'); | |
} catch (FacebookApiException $e) { | |
error_log($e); | |
$user = null; | |
} | |
//LOGOUT URL (IF YOU NEED AN URL FOR THE LOGOUT BUTTON) | |
$logoutUrl = $facebook->getLogoutUrl(); | |
}else{ | |
//REDIRECT TO LOGIN/ADD APP URL | |
echo "<script type='text/javascript'>top.location.href='$loginUrl';</script>"; | |
exit; | |
} | |
//GET THE ACCESS TOKEN | |
$access_token = $facebook->getAccessToken(); | |
//example: SHOW AVATAR | |
echo "<img src='https://graph.facebook.com/".$user_profile['id']."/picture'><br/>"; | |
//example: WRITE ON USER'S WALL | |
$url = "https://graph.facebook.com/".$user_profile["id"]."/feed"; | |
$fields = "access_token=".$access_token."&message=publish to wall using Facebook PHP SDK"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, 2); | |
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN THE CONTENTS OF THE CALL | |
$rst_data = curl_exec($ch); | |
curl_close($ch); | |
if(!is_null($rst_data)) echo "<h3>WRITIN ON WALL SUCCESFUL.</h3>"; | |
//DISPLAY THE user_profile ARRAY | |
echo "<pre>"; | |
var_dump($user_profile); | |
echo "</pre>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment