Last active
January 11, 2022 08:15
-
-
Save nmsdvid/5388799 to your computer and use it in GitHub Desktop.
Facebook API - using the latest Facebook API with success
This file contains hidden or 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
Check page tab's / canvas page's Like status via Facebook PHP SDK | |
//this goes to the top of the PHP document | |
function parsePageSignedRequest() { | |
if (isset($_REQUEST['signed_request'])) { | |
$encoded_sig = null; | |
$payload = null; | |
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2); | |
$sig = base64_decode(strtr($encoded_sig, '-_', '+/')); | |
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true)); | |
return $data; | |
} | |
return false; | |
} | |
if($signed_request = parsePageSignedRequest()) { | |
if($signed_request->page->liked) { | |
//if the page is already liked | |
//HTML,JS, etc.. content goes here | |
} else { | |
//if the page is NOT liked | |
//HTML,JS, etc.. content goes here | |
} | |
} | |
This file contains hidden or 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
//Call the Facebook JavaScript SDK | |
<div id="fb-root"></div> | |
<script> | |
// Called when FB SDK has been loaded | |
window.fbAsyncInit = function() { | |
FB.init({ | |
appId : 'XXXXXXXXX', // App ID | |
status : true, // check login status | |
cookie : true, // enable cookies to allow the server to access the session | |
xfbml : true // parse XFBML | |
}); | |
}; | |
// Load the SDK Asynchronously | |
(function (d) { | |
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; } | |
js = d.createElement('script'); js.id = id; js.async = true; | |
js.src = "//connect.facebook.net/en_US/all.js"; | |
d.getElementsByTagName('head')[0].appendChild(js); | |
} (document)); | |
</script> |
This file contains hidden or 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
Login the user to get user data, please note that you MUST use Canvas aka App on Facebook and not Page Tab for this to work properly. | |
<div id="fb-root"></div> | |
<script> | |
// Facebook SDK | |
window.fbAsyncInit = function() { | |
FB.init({ | |
appId : 'XXXXXX', // App ID | |
channelUrl : 'http://yourhost.com/channel.html', // Channel File | |
status : true, // check login status | |
cookie : true, // enable cookies to allow the server to access the session | |
xfbml : true // parse XFBML | |
}); | |
// First we need to check if the user is already logged in | |
FB.getLoginStatus(function(response) { | |
if (response.status === 'connected') { | |
// If user is logged in call the callAPI() function | |
callAPI(); | |
} else if (response.status === 'not_authorized') { | |
// If the user is not logged in call the login() function | |
login(); | |
} else { | |
// if the status is unknown do something, in this case we call the login() function | |
login(); | |
} | |
}); | |
}; | |
//login the user | |
function login() { | |
FB.login(function(response) { | |
if (response.authResponse) { | |
// If user is accepted the permission call the callAPI() function | |
callAPI(); | |
} else { | |
// If the user declined the permission do something, in this case lets refresh the page. | |
window.parent.location = "http://myhost.com"; | |
} | |
},{scope: 'publish_actions'}); // in some cases you need to specify the permissions, a nice list is here: | |
http://developers.facebook.com/docs/reference/login/#permissions | |
} | |
//If the user accapted the premission this is the function waht will be called | |
function callAPI() { | |
FB.api('/me', function(response) { | |
//now we can access the the user informations like this: | |
response.id // get users Facebook ID | |
response.username // get users Facebook username | |
} | |
</script> |
This file contains hidden or 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
// Manipulate iFrame height with FB.Canvas.setSize and FB.Canvas.setAutoGrow(); | |
simple example: | |
<div id="fb-root"></div> | |
<script> | |
// Facebook SDK | |
window.fbAsyncInit = function() { | |
FB.init({ | |
appId : 'XXXXX', // App ID | |
channelUrl : 'http://yourhost.com/channel.html', // Channel File | |
status : true, // check login status | |
cookie : true, // enable cookies to allow the server to access the session | |
xfbml : true // parse XFBML | |
}); | |
//Put the FB.Canvas.setSize and FB.Canvas.setAutoGrow() functions here | |
//To set a fixed size, use the FB.Canvas.setSize like this: | |
FB.Canvas.setSize({width:810, height:775}); | |
//To create a fluid height simply use the FB.Canvas.setAutoGrow() function | |
FB.Canvas.setAutoGrow(); | |
}; | |
</script> | |
Bonus: to change/modify the height on click, etc.. use this code: | |
FB.Canvas.setSize({height:1500}); | |
setTimeout("FB.Canvas.setAutoGrow()",500); | |
This file contains hidden or 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
Post on the users FB wall, please note that the user needs to be logged in to make this work. | |
function postText() { //Post on the users fb wall | |
var params = {}; | |
params['message'] = 'This is the Message'; | |
params['name'] = 'Name'; | |
params['link'] = 'link to be shared, for example: http://myhost.com'; | |
params['picture'] = 'specify the image path, use direct url, for example: http://myhost.com/img.jpg '; | |
params['description'] = 'Short description'; | |
FB.api('/me/feed', 'post', params, function(response) { | |
if (!response || response.error) { | |
//if the sharing went wrong | |
console.log('error'); | |
console.log(response); | |
} else { | |
//if everything worked out well | |
console.log('ok'); | |
console.log(response); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment