When developing website applications, you may need khổng lồ connect to lớn the Facebook API lớn gather information or make posts for the user. In this article, we will be teaching you how to connect to 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 lớn prepare the SDK on your development environment by following our guide onobtaining the Facebook SDK.
Full code example
validate() ) $session = null;}// kiểm tra if an active session exists.if ( !isset( $session ) || $session === null ) // If no session exists, let"s try khổng lồ 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 to lớn the Facebook SDK
First, we will need khổng lồ connect to the Facebook SDK khổng lồ 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 khổng lồ include it và 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 lớn include the autoload.php file.
Then, as our connection needs khổng lồ load a session with Facebook, we have set our code khổng lồ include theFacebookSession,FacebookRedirectLoginHelper, andFacebookRequestPHP classes to lớn appropriately connect as well as request data from Facebook’s API.
Xem thêm: Bà Bầu Có Thai Ăn Mực Được Không ? Bà Bầu Ăn Mực Được Không
Starting the session
Now that we have included the required libraries in our code, we need to open a new session. To vày so, simply call thesession_star()function:
// Initialize the Facebook ứng dụng using the application ID and secret.FacebookSession::setDefaultApplication( "xxxxxxxxxxxxxx","xxxxxxxxxxxxxxxxxxxxxxxxxxx" );Within this line, you will need to địa chỉ your Application ID và Secret that is obtained from the Facebook Developers site. If you vì not already have an Application ID, you may follow our article on obtaining a Facebook API key.
Now that the application is initialized và your credentials are verified with the Facebook API, a session needs to be created with Facebook. When creating sessions with the Facebook API, the user will be sent to lớn Facebook to lớn log in và authorize the application, then redirected back lớn your application. In the following lines, we are opening a session with Facebook & preparing lớn run requests to the Facebook API.
// Define Facebook"s login helper & redirect back lớn our page.$helper = new FacebookRedirectLoginHelper( "https://local.wordpress.dev/fb/index.php" );// kiểm tra to ensure our session was started correctly and 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 lớn 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 nội dung appopriately. If the user is not yet logged in và provided permissions lớn the application, the user will be prompted with a liên kết to log in and will be returned lớn the page as per theFacebookRedirectLoginHelperURL. Once the user is returned, they will have a session active & will then be shownConnected lớn 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 lớn Facebook!"; else // Show login urlecho "getLoginUrl( array( "email", "user_friends" ) ) . "">Login";
Where bởi vì I go from here?
Now that you know how lớn easily connect lớn the Facebook API using the Facebook SDK for PHP, you can now make queries to Facebook khổng lồ generate information on users.

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 file on the public.html root directory. Should the autoload.php và index.php be placed (or code added) to lớn those on the root drive? Would I also then need to change some code to lớn make sure it pulls up the rest of the information left in the facebook thư mục I created with the other SDK files?