Connecting To The Facebook Api Using The Facebook Php Sdk

When developing web applications, you may need khổng lồ connect to lớn the Facebook API to lớn gather information or make posts for the user. In this article, we will be teaching you how to connect to lớn the Facebook API using the Facebook PHP SDK.

Bạn đang xem: Connecting to the facebook api using the facebook php sdk

Note: If you have not already done so, be sure to prepare the SDK on your development environment by following our guide onobtaining the Facebook SDK.

Full code example

validate() ) $session = null;}// check if an active session exists.if ( !isset( $session ) || $session === null ) // If no session exists, let"s try lớn create one.$session = $helper->getSessionFromRedirect();// Make sure we have a session started.if ( isset( $session ) ) // Save the session$_SESSION<"fb_token"> = $session->getToken();// Create a new Facebook session using our token.$session = new FacebookSession( $session->getToken() );echo "Connected lớn Facebook!"; else // Show login urlecho "getLoginUrl( array( "email", "user_friends" ) ) . "">Login";

Walking through the code

Connecting khổng lồ the Facebook SDK

First, we will need lớn connect khổng lồ the Facebook SDK to load all of the required libraries. Thankfully, much of the work is already done for us with theautoload.phpfile that is included with the Facebook SDK. We simply need to include it and reference the required connection classes like so:

// Begin loading the necessary files for the Facebook SDK.require ( "autoload.php" );// Load the required PHP classes from the Facebook SDK.use FacebookFacebookSession;use FacebookFacebookRedirectLoginHelper;use FacebookFacebookRequest;As you can see here, we used therequire()PHP function to include the autoload.php file.

Then, as our connection needs khổng lồ load a session with Facebook, we have set our code to include theFacebookSession,FacebookRedirectLoginHelper, andFacebookRequestPHP classes khổng lồ appropriately connect as well as request data from Facebook’s API.

Xem thêm: Nên Học Php Hay Ruby Hay Php? Tại Sao Web Designer Nên Học Ruby On Rails

Starting the session

Now that we have included the required libraries in our code, we need to xuất hiện a new session. To vì so, simply call thesession_star()function:

// Initialize the Facebook tiện ích using the application ID và secret.FacebookSession::setDefaultApplication( "xxxxxxxxxxxxxx","xxxxxxxxxxxxxxxxxxxxxxxxxxx" );Within this line, you will need to địa chỉ cửa hàng your Application ID và Secret that is obtained from the Facebook Developers site. If you vày not already have an Application ID, you may follow our article on obtaining a Facebook API key.

Now that the application is initialized and your credentials are verified with the Facebook API, a session needs to lớn be created with Facebook. When creating sessions with the Facebook API, the user will be sent khổng lồ Facebook khổng lồ log in và authorize the application, then redirected back to lớn your application. In the following lines, we are opening a session with Facebook và preparing khổng lồ run requests to lớn the Facebook API.

// Define Facebook"s login helper & redirect back to our page.$helper = new FacebookRedirectLoginHelper( "https://local.wordpress.dev/fb/index.php" );// kiểm tra to ensure our session was started correctly và the access token exists.if ( isset( $_SESSION ) && isset( $_SESSION<"fb_token"> ) ) // Using the access token, create a new session.$session = new FacebookSession( $_SESSION<"fb_token"> );// Determine if the defined session is indeed valid.if ( !$session->validate() ) $session = null;// check if an active session exists.if ( !isset( $session ) || $session === null ) // If no session exists, let"s try to lớn create one.$session = $helper->getSessionFromRedirect();

Prompt the user to login or display data

In the next lines, we then need to kiểm tra to see if there is an active session, then display the content appopriately. If the user is not yet logged in & provided permissions khổng lồ the application, the user will be prompted with a links to log in và will be returned to lớn the page as per theFacebookRedirectLoginHelperURL. Once the user is returned, they will have a session active and will then be shownConnected khổng lồ Facebook!in their browser.

// Make sure we have a session started.if ( isset( $session ) ) // Save the session$_SESSION<"fb_token"> = $session->getToken();// Create a new Facebook session using our token.$session = new FacebookSession( $session->getToken() );echo "Connected khổng lồ Facebook!"; else // Show login urlecho "getLoginUrl( array( "email", "user_friends" ) ) . "">Login";

Where vị I go from here?

Now that you know how to easily connect to lớn the Facebook API using the Facebook SDK for PHP, you can now make queries to lớn Facebook lớn generate information on users.


*
praharsh says:
*
John says:

I haven’t received any errors, however I just don’t have the facebook button appearing. As for the index file, I made a separate one which is located within the same thư mục as the autoload.php file. Opencart appears to have another index tệp tin on the public.html root directory. Should the autoload.php and index.php be placed (or code added) to those on the root drive? Would I also then need to change some code to make sure it pulls up the rest of the information left in the facebook thư mục I created with the other SDK files?