Using PHP to invoke Facebook api
IPFS
I really feel that the official document website of Facebook api is very bad, and then the usage of PHP api is too complicated. It is better to use curl directly in PHP.
The usage of Facebook api through php, the ideal process is roughly like this
- First through the login page (eg login.php), after obtaining a token, for the call back page (eg fb-callback.php) use
- After each token is used, the returned data will contain the token that should be used next time, and the original token will be expired.
login authorization
<?php session_start(); require_once('PATH_TO_AUTOLOAD.PHP'); $fb = new Facebook\Facebook([ 'app_id' => 'APP_ID', 'app_secret' => 'APP_SECRET', 'default_graph_version' => 'v2.8' ]); $helper = $fb->getRedirectLoginHelper(); $permissions = ['email', 'publish_actions', 'user_friends']; //permissions want to use $loginUrl = $helper->getLoginUrl('http://www.example.com/fb-callback.php', $permissions); //call back url echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>'; ?>
Call back processing, get the token to do things, the following example is to use curl to get the friend list (taggable friends)
<?php session_start(); require_once('PATH_TO_AUTOLOAD.PHP'); $fb = new Facebook\Facebook([ 'app_id' => 'APP_ID', 'app_secret' => 'APP_SECRET', 'default_graph_version' => 'v2.8' ]); $helper = $fb->getRedirectLoginHelper(); try { $accessToken = $helper->getAccessToken(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch(Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } if (!isset($accessToken)) { if ($helper->getError()) { header('HTTP/1.0 401 Unauthorized'); echo "Error: " . $helper->getError() . "\n"; echo "Error Code: " . $helper->getErrorCode() . "\n"; echo "Error Reason: " . $helper->getErrorReason() . "\n"; echo "Error Description: " . $helper->getErrorDescription() . "\n"; } else { header('HTTP/1.0 400 Bad Request'); echo 'Bad request'; } exit; } // The OAuth 2.0 client handler helps us manage access tokens $oAuth2Client = $fb->getOAuth2Client(); $url = "https://graph.facebook.com/v2.8/me/taggable_friends?fields=name,picture&limit=5&access_token=" . (string)$accessToken; $headers = array("Content-type: application/json"); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko"); $st = curl_exec($ch); $result = json_decode($st, TRUE); foreach ($result['data'] as $friend) { echo '<center><img src="' . $friend['picture']['data']['url'] . '"><br>' . $friend['name'] . '</center ><br>'; } ?>
ps Facebook's php api is here , or go directly to github .
Original link: Phanix's Blog
Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!