Facebook PHP SDK

Facebook PHP SDK provides Facebook Platform support to your PHP-based apps. It also integrates with Javascript SDK to provide seamless server and client-side authentication. The SDK is open source!

Now, I show you how to create a new app on Facebook to get the user's picture and name.

Download PHP SDK

First download PHP SDK from GitHub. You will need the files from src directory.

Create New App

Go to developers.facebook.com/apps click on Create New App button.
Add a name, like 1stWebMagazine Login and click next.
At Select how your app integrates with Facebook click on Website with a Facebook Login, type your site URL eg. http://1stwebmagazine.com/demo/.
Your app is ready to use!

Get and print the User's profile name and picture via the Graph API

Insert your App ID and App Secret to YOUR_APP_ID and YOUR_APP_SECRET in the following code and save it as index.php.

<?php
	// Remember to copy files from the SDK's src/ directory to a
	// directory in your application on the server, such as src/
	require_once('src/facebook.php');
	
	// Insert your App ID and App Secret
	$config = array(
		'appId' => 'YOUR_APP_ID',
		'secret' => 'YOUR_APP_SECRET',
	);
	
	$facebook = new Facebook($config);
	$user_id = $facebook->getUser();
   
?>

<html>
<head></head>
<body>

<?php
if($user_id) {

// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {

    $user_profile = $facebook->api('/me','GET');
    echo '
'; echo $user_profile['name']; } catch(FacebookApiException $e) { // If the user is logged out, you can have a // user ID even though the access token is invalid. // In this case, we'll get an exception, so we'll // just ask the user to login again here. $login_url = $facebook->getLoginUrl(); echo 'Please <a href="' . $login_url . '">login.</a>'; error_log($e->getType()); error_log($e->getMessage()); } } else { // No user, print a link for the user to login $login_url = $facebook->getLoginUrl(); echo 'Please <a href="' . $login_url . '">login.</a>'; } ?> </body> </html>

Copy index.php and the src directory from the PHP SDK to the that folder on your website what you entered earlier.

 

Demo »


Go Daddy


Related articles

Email Validation

Email Validation

08/02/2013

Learn how to validate email on client side with HTML5, Javascript and on server side with PHP.

Generate Thumbnail on the Fly with PHP

Generate Thumbnail on the Fly with PHP

04/02/2013

In a previous artice I already showed you how to generate square thumbnails with PHP. Now I show you how to keep the original ratio of the image and how to control the alignment of the square image.

Facebook Upload Photo to User′s Profile

Facebook Upload Photo to User′s Profile

31/01/2013

Learn how to upload a photo to Facebook user′s profile with PHP SDK via Graph API!


Leave a comment

We welcome any comment from you! Please keep in mind that comments are moderated and rel='nofollow' is in use. So, please do not use a domain as your name or a spammy keyword, or your comment will be deleted.

Name: *

E-mail: *

Message: *

Click on the apple *


Find us on Facebook

Tags

Get online with the world's Best - GoDaddy.com!

Back to Top