Tweet |
In this post I will explain how to connect your website visitors with their Facebook accounts , so you don't need to prompt your users with registration forms or login forms , just a button to login with Facebook or connect with Facebook and you are done .
You can save your users Facebook data in your database , also you can save their Facebook login status in cookies not to prompt them every time with the login dialog .
I will also explain how to post on Facebook users timeline , also post on the pages they own ( or the pages in which they are admin ) .
Also the post can include photos or videos like any post you put on facebook.
Let's begin .
First you have to create a new app , go to http://developers.facebook.com/apps and then click on "Create New App" button , choose a unique name and then click Create .
You can choose free webhosting if you don't have a host .
You will be redirected to a page where you can edit your app settings , to visit this page again , you can go to http://developers.facebook.com/apps you will find your apps on the left , and Edit Settings link on the right .
The first page is Basic page , you can edit general information about your app , the most important thing you have to check is "App Domains" , you can enter several domains , but you must have control on them.
Also in the Basic page you have you app ID and app Secret , grab them and save them for now .
The second page is Basic > Permissions , you have 2 fields to edit ( User & Friend Permissions ) and ( Extended Permissions ) , you will find a list of available permissions here ( http://developers.facebook.com/docs/reference/login/#permissions ) .
If you need access to the users friends list , you have to add read_friendlists to extended permissions .
If you want to post on their behalf , you should add publish_stream .
And if you need to manage their pages , you have to add manage_pages to extended permissions.
Go now and download from github the facebook php sdk ( https://github.com/facebook/facebook-php-sdk ) , copy the src folder to somewhere included in your website .
Go to your index.php page , or any page you need to be accessed by facebook and type the following.
First include facebook sdk file
Next create new instance from this classYou can save your users Facebook data in your database , also you can save their Facebook login status in cookies not to prompt them every time with the login dialog .
I will also explain how to post on Facebook users timeline , also post on the pages they own ( or the pages in which they are admin ) .
Also the post can include photos or videos like any post you put on facebook.
Let's begin .
First you have to create a new app , go to http://developers.facebook.com/apps and then click on "Create New App" button , choose a unique name and then click Create .
You can choose free webhosting if you don't have a host .
You will be redirected to a page where you can edit your app settings , to visit this page again , you can go to http://developers.facebook.com/apps you will find your apps on the left , and Edit Settings link on the right .
The first page is Basic page , you can edit general information about your app , the most important thing you have to check is "App Domains" , you can enter several domains , but you must have control on them.
Also in the Basic page you have you app ID and app Secret , grab them and save them for now .
The second page is Basic > Permissions , you have 2 fields to edit ( User & Friend Permissions ) and ( Extended Permissions ) , you will find a list of available permissions here ( http://developers.facebook.com/docs/reference/login/#permissions ) .
If you need access to the users friends list , you have to add read_friendlists to extended permissions .
If you want to post on their behalf , you should add publish_stream .
And if you need to manage their pages , you have to add manage_pages to extended permissions.
Go now and download from github the facebook php sdk ( https://github.com/facebook/facebook-php-sdk ) , copy the src folder to somewhere included in your website .
Go to your index.php page , or any page you need to be accessed by facebook and type the following.
First include facebook sdk file
require_once 'src/facebook.php' ;
$facebook = new Facebook(array('appId' =>APP_ID, 'secret' =>APP_SECRET ));APP_ID and APP_SECRET can be got from the basic page of your facebook app ( http://developers.facebook.com/apps ) .
Second you need to check if the user is logged in and has authorised your application or not , this can be done using the following line .
$facebook->getUser();This function check if the user is logged in and authorised to your app or not , and is saving the logging data in session
If the user is not logged in , we should print a login url to connect the user with facebook .
if(!$facebook->getUser()){ printf('Connect With Facebook') , $facebook->getLoginUrl(array("scope" => "manage_pages,publish_stream"))); }You should add the scope ( or the permissions ) you have included from the settings page in your app , in the login url that the user will go and click and authorise your app , so he ( the user ) will authorise your app for the permissions included .
The user should be prompted with something like that .
If the user authorised your app , he should be redirected back to your website so you can check again if he logged in or not.
if(!$facebook->getUser()){ printf('Connect With Facebook') , $facebook->getLoginUrl(array("scope" => "manage_pages,publish_stream"))); } else { // the user is logged in and authorised the app // we can grab the user account data . $userData = $facebook>api('/me'); printf ( 'Welcome %s' , $userData["name"]); }If you need to publish a post on behalf of the user , remember that you need "publish_stream" permission
$params = array("message" => "Testing the new facebook sdk"); $facebook->api("USER_ID/feed" , 'POST' , $params);You can get User_ID from $userData ( $facebook->api('/me') );
$params is associative array of the post parameters , you can get full description of the parameters here ( https://developers.facebook.com/docs/reference/api/post/ ) .
If you need a list of the pages the user owns or admins
$pages = $facebook->api("/me/accounts");I hope that will be useful for anyone want to use the facebook php sdk .
0 comments: