You can still use the obsolete (but still working) facebookarchive / facebook-php-sdk .
And I donβt think this will change soon, because this code has been forked over 3,000 times on Github, and probably another ten thousand use it.
Here, successful user information is retrieved from the old PHP PHP SDK (tested on my server with PHP 5.3) -
It's 2016 and it still works - both as a Canvas callback and as an OAuth callback:
<?php require_once('facebook-php-sdk-3.2.3/src/facebook.php'); const TITLE = 'My amazing app'; const REDIRECT = 'https://example.com/myapp/'; $client = new Facebook(array( 'appId' => REPLACE_ME, 'secret' => REPLACE_ME, )); if (isset($_REQUEST['logout'])) { $client->destroySession(); header('Location: ' . REDIRECT); exit(0); } if ($client->getUser()) { try { $me = $client->api('/me?fields=id,first_name,gender'); $body = '<PRE>' . print_r($me, TRUE) . '</PRE>'; } catch (FacebookApiException $ex) { error_log($ex); $body = '<PRE>' . htmlspecialchars($e->getMessage()) . '</PRE>'; } } else { $body = sprintf('<P><A HREF="%s">Login</A></P>', $client->getLoginUrl()); } ?> <!DOCTYPE HTML> <HTML> <HEAD> <TITLE><?= TITLE ?></TITLE> </HEAD> <BODY> <?= $body ?> <P><A HREF="<?= REDIRECT ?>?logout">Logout</A></P> </BODY> </HTML>
Do not forget -
- Get app id and secret in Facebook console
- Register the
https://example.com/myapp/ callback as Canvas (and also optionally as a valid OAuth2 redirect URL) in the same place
Another alternative could be to use the Facebook SDK for JavaScript (here with jQuery):
<!DOCTYPE HTML> <HTML> <HEAD> <TITLE><?= TITLE ?></TITLE> <SCRIPT SRC="//ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></SCRIPT> <SCRIPT> $(document).ready(function() { </SCRIPT> </HEAD> <BODY> <?= $body ?> <P><A HREF="<?= REDIRECT ?>?logout">Logout</A></P> </BODY> </HTML>
However, using the PHP SDK is preferable - because the information already exists (via POST or HTTP redirection), and since the JavaScript SDK shows a login pop-up window or darkens the page every time the page loads and actually requires the user to click it without browser blocking.
source share