HomePHP Page 4 - Facebook PHP API Applications: A Second Look
Final Facebook PHP API Script - Display basic user information - PHP
This is the second part of the Facebook PHP API tutorial. The first part ran on August 11. It covered the most basic features, such as adding the basic Facebook “Like” button, the “Recommendations” plugin and registering your applications in Facebook to get an Application ID and Secret key. In this part, we'll delve deeper.
Below is the final, working PHP script that will communicate to the Facebook API and retrieve very basic user information. You can also download the script at the link.
<?php define('FACEBOOK_APP_ID', '***YOUR FACEBOOK APPLICATION ID HERE***'); define('FACEBOOK_SECRET', '***YOUR FACEBOOK SECRET APPS KEY HERE***'); function get_facebook_cookie($app_id, $application_secret) { $args = array(); parse_str(trim($_COOKIE['fbs_' . $app_id], '"'), $args); ksort($args); $payload = ''; foreach ($args as $key => $value) { if ($key != 'sig') { $payload .= $key . '=' . $value; } } if (md5($payload . $application_secret) != $args['sig']) { return null; } return $args; } $cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET); ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"> <body> <?php if ($cookie) { //cookie is set, user is logged in $user = json_decode(file_get_contents('https://graph.facebook.com/'.$cookie['uid'])); //Display the facebook user ID, name, gender and Facebook URL in the web browser echo '<br />'; echo 'Your Facebook ID: '.$user->{'id'}; echo '<br />'; echo 'Your name: '.$user->{'name'}; echo '<br />'; echo 'Your gender: '.$user->{'gender'}; echo '<br />'; echo 'Your Facebook URL: '.$user->{'link'}; echo '<br />'; echo '<fb:login-button autologoutlink="true"></fb:login-button>'; } else { //user is not logged in, display the Facebook login button echo '<h2>Facebook Application Test page</h2>'; echo '<br />'; echo 'This is the most basic Facebook application PHP source code that will grab the user Facebook full name, gender and Facebook URL.'; echo '<br />Then displays those information in the web browser once the user has successfully logged in'; echo '<br /><br />'; echo '<fb:login-button autologoutlink="true"></fb:login-button>'; } ?> <div id="fb-root"></div> <script src="http://connect.facebook.net/en_US/all.js"></script> <script> FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true, cookie: true, xfbml: true}); FB.Event.subscribe('auth.login', function(response) { window.location.reload(); }); </script> </body> </html>
How to implement the script on your website
First, make sure your website and your account complies with the basic requirements outlined in the first part of this tutorial.
Second, you need to get your own application ID and apps secret key, which you need to define in the above source code.
Third, you need to save your PHP application (for example, my_facebook_api.php)
Fourth, upload the script into your website server. For testing purposes, start testing it at the server root directory. For example: http://www.php-developer.org/my_facebook_api.php
Fifth, launch the application URL (http://www.php-developer.org/my_facebook_api.php) in the browser. Try logging in first by using your own Facebook account to see if it provides any errors.
This is just a very basic implementation to show how the Facebook API works using PHP. In the upcoming Facebook API tutorials, you will learn how to grab detailed user information, such as friends, photos, and so forth. Details for creating your own Facebook apps, which can be hosted on your website and can be used by other Facebook users, will also be covered.