-
-
Save josep112/3794680cfcdbd03fdb8a8c012922c157 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Recebe o token e publica na page. | |
* Facebook SDK version 2.5 | |
* | |
* @author Thiago A. de Lima <[email protected]> | |
*/ | |
require_once 'Facebook/autoload.php'; | |
var $appId = 'xxxxxx'; | |
var $appSecret = 'xxxxxx'; | |
var $pageId = 'xxxxxx'; | |
$fb = new \Facebook\Facebook([ | |
'app_id' => $appId, | |
'app_secret' => $appSecret, | |
'default_graph_version' => 'v2.5' | |
]); | |
$helper = $fb->getRedirectLoginHelper(); | |
try { | |
$accessToken = $helper->getAccessToken(); | |
} | |
catch (Facebook\Exceptions\FacebookResponseException $e) { | |
echo 'Graph returned an error: ' . $e->getMessage(); | |
exit; | |
} | |
catch (Facebook\Exceptions\FacebookSDKException $e) { | |
echo 'Facebook SDK returned an error: ' . $e->getMessage(); | |
exit; | |
} | |
if (!isset($accessToken)) { | |
echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.'; | |
exit; | |
} | |
/* | |
* Para realizar a publicação na Page, é necessário capturar o token de acesso da page. | |
*/ | |
function getPageToken($pageId, $accessToken){ | |
$uri = 'https://graph.facebook.com/v2.5/'.$pageId.'?access_token='.$accessToken; | |
$uri .= '&fields=access_token&format=json&method=get&pretty=0&suppress_http_code=1'; | |
$response = json_decode(file_get_contents($uri)); | |
return $response->access_token; | |
} | |
$pageAccessToken = getPageToken($pageId, $accessToken); | |
/* Monto os dados que serão publicados */ | |
$data = array( | |
'link' => 'http://google.com', | |
'message' => 'Texto que será publicado!', | |
'published' => true, | |
'access_token' => $pageAccessToken | |
); | |
/* Envio os dados para a page */ | |
try { | |
$response = $fb->post('/'.$pageId.'/feed', $data, $pageAccessToken); | |
} | |
catch(Facebook\Exceptions\FacebookResponseException $e) { | |
echo $e->getMessage(); | |
} | |
catch(Facebook\Exceptions\FacebookSDKException $e) { | |
echo $e->getMessage(); | |
} |
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 | |
/** | |
* Gera o token de acesso ao Facebook | |
* Facebook SDK version 2.5 | |
* | |
* @author Thiago A. de Lima <[email protected]> | |
*/ | |
require_once 'Facebook/autoload.php'; | |
var $appId = 'xxx'; | |
var $appSecret = 'xxxxxx'; | |
var $callbackURI = 'callback.php'; | |
$fb = new \Facebook\Facebook([ | |
'app_id' => $appId, | |
'app_secret' => $appSecret, | |
'default_graph_version' => 'v2.5' | |
]); | |
$helper = $fb->getRedirectLoginHelper(); | |
$permissions = ['manage_pages', 'publish_pages', 'publish_actions']; | |
$loginUrl = $helper->getLoginUrl($callbackURI, $permissions); | |
header('Location: '.$loginUrl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment