Last active
November 17, 2015 08:09
-
-
Save thanhhh/8e691f2e04d8559b4654 to your computer and use it in GitHub Desktop.
Facebook PHP SDK Sample: Post As A Page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use Facebook\Authentication\AccessToken; | |
use Facebook\FacebookApp; | |
use Facebook\FacebookRequest; | |
session_start(); | |
$app_id = '{APP ID}'; | |
$app_secret = '{APP SECRET}'; | |
$app = new FacebookApp($app_id, $app_secret); | |
$fb = new Facebook\Facebook(array( | |
'app_id' => $app_id, | |
'app_secret' => $app_secret, | |
'default_graph_version' => 'v2.5', | |
)); | |
//Page access token has been got from get_page_access_token.php | |
$access_token = '{PAGE ACCESS TOKEN}'; | |
$page_id = '{PAGE ID}'; | |
$post_data = array( | |
'message' => '{Your message data here}' | |
); | |
$request = new FacebookRequest($app, $access_token, 'POST', '/' . $page_id . '/feed', $post_data); | |
// Send the request to Graph | |
try { | |
$response = $fb->getClient()->sendRequest($request); | |
$graphNode = $response->getGraphNode(); | |
echo 'Post ID: ' . $graphNode['id']; | |
} 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
session_start(); | |
$fbData = array( | |
'app_id' => '{APP ID}', | |
'app_secret' => '{APP SECRET}', | |
'default_graph_version' => 'v2.2' | |
); | |
$fb = new Facebook\Facebook($fbData); | |
$helper = $fb->getRedirectLoginHelper(); | |
try { | |
$accessToken = $helper->getAccessToken(); | |
// OAuth 2.0 client handler | |
$oAuth2Client = $fb->getOAuth2Client(); | |
// Exchanges a short-lived access token for a long-lived one | |
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); | |
echo "Access Token: " . $accessToken; | |
} 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use Facebook\Authentication\AccessToken; | |
use Facebook\FacebookApp; | |
use Facebook\FacebookRequest; | |
session_start(); | |
$app_id = '{APP ID}'; | |
$app_secret = '{APP SECRET}'; | |
$app = new FacebookApp($app_id, $app_secret); | |
$fb = new Facebook\Facebook(array( | |
'app_id' => $app_id, | |
'app_secret' => $app_secret, | |
'default_graph_version' => 'v2.5', | |
)); | |
$page_id = '{PAGE ID}'; | |
//User access token get from cakllback.php | |
$access_token = new AccessToken('{USER ACCESS TOKEN}'); | |
$request = new FacebookRequest($app, $access_token, 'GET', '/' . $page_id, array( 'fields' => 'access_token') ); | |
// Send the request to Graph | |
try { | |
$response = $fb->getClient()->sendRequest($request); | |
$graphNode = $response->getGraphNode(); | |
echo 'Page Access Token: ' . $graphNode['access_token']; | |
} 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
session_start(); | |
$fbData = array( | |
'app_id' => '{APP ID}', | |
'app_secret' => '{APP SECRET}', | |
'profile_id' => '{PAGE ID}', | |
'default_graph_version' => 'v2.5', | |
); | |
$fb = new Facebook\Facebook($fbData); | |
$params = array('req_perms' => 'manage_pages, publish_pages, public_profile'); | |
$helper = $fb->getRedirectLoginHelper(); | |
$loginUrl = $helper->getLoginUrl('http://fbsamples.local/callback.php', $params); | |
header('Location: '. $loginUrl); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment