Skip to content

Instantly share code, notes, and snippets.

@vikramacharya
Created April 12, 2012 16:47
Show Gist options
  • Select an option

  • Save vikramacharya/2369019 to your computer and use it in GitHub Desktop.

Select an option

Save vikramacharya/2369019 to your computer and use it in GitHub Desktop.
<?php
$fbconfig['appid' ] = "XXXXX"; //put your api key here
$fbconfig['secret'] = "XXXXXXXXXXXXXXXXXX"; // put your api secret key here
$fbconfig['baseurl'] = "http://myexample.com/index.php"; //put your host address here
if (isset($_GET['request_ids']))
{
}
$user = null;
try
{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri' => $fbconfig['baseurl']
)
);
$logoutUrl = $facebook->getLogoutUrl();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}
//if user is logged in
if ($user){
//get user basic description
$userInfo = $facebook->api("/$user");
//update user's status using graph api
//http://developers.facebook.com/docs/reference/dialogs/feed/
if (isset($_GET['publish'])){
try {
$publishStream = $facebook->api("/$user/feed", 'post', array(
'message' => "I like this app development tutorials by social media development)",
'link' => 'http://vikramacharya.com',
'picture' => '',
'name' => 'Apps & Games',
'description'=> 'Apps and games!'
)
);
//as $_GET['publish'] is set so remove it by redirecting user to the base url
} catch (FacebookApiException $e) {
d($e);
}
$redirectUrl = $fbconfig['baseurl'] . '/index.php?success=1';
header("Location: $redirectUrl");
}
//update user's status using graph api
//http://developers.facebook.com/docs/reference/dialogs/feed/
if (isset($_POST['tt'])){
try {
$statusUpdate = $facebook->api("/$user/feed", 'post', array('message'=> $_POST['tt']));
} catch (FacebookApiException $e) {
d($e);
}
}
//fql query example using legacy method call and passing parameter
try{
$fql = "select name, hometown_location, sex, pic_square from user where uid=" . $user;
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
}
catch(Exception $o){
d($o);
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment