File: D:/HostingSpaces/SBogers42/tandartsmaas.nl/app/Komma/Ratings/FacebookService.php
<?php
namespace Komma\Ratings;
use Facebook\Authentication\AccessToken;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Facebook;
class FacebookService
{
private $facebook;
private $appId = '1607477276198130';
private $appSecret = 'e5736586c1efa912705e4256ff7cce68';
private $accessToken = 'EAAW1ZCfgIRPIBACNKiw3qFAjD2qZAX279pkW3EZCrokR6a9QRjRrjZBTo9h2lr8Dq7ZAjmHfSq3OKkjwDpZC49LdyPQRTJOolUFC4JzpaGlOweQAK0PZCuyyMTupsPL6e1DCRx3ufkwhsgOGBugyE0x7VOjWCNNZAf9JcdyVdy0k5gI63XoqNUSS1zqhvSgcfuqzk5GWWnOB2QZDZD';
private $redirectUrl = 'http://tandartsmaas.komma.pro/facebook/getPageToken';
// private $pageToken = 'CAAW1ZCfgIRPIBAINl2ioxnNCGwIapJCf44ihUndBEqutb8KrCucxQeqnuQgOTOgWDh7ZCrYDLeEFLDqMALZBZCYkf2rEZBeOBdxHZBVUFKpeeaYcHZCPyDah79DdpMcMHLekPRTAWduydlvsKstfDr3YE52c5EZASVyQfugsV1NDffuzRWVZAqOgG';
public function __construct()
{
$accessToken = new AccessToken($this->accessToken);
$this->facebook = new Facebook([
'app_id' => $this->appId,
'app_secret' => $this->appSecret,
'default_graph_version' => 'v2.10',
'default_access_token' => $accessToken
]);
// dd( $this->facebook );
//dd( $this->facebook->get('/me/accounts') );
//https://developers.facebook.com/docs/facebook-login/access-tokens
//https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension#long-via-code
}
/*
* Represents a Facebook Session, which is used when making requests to the Graph API.
* Get session for app-level requests:
*/
// public function session()
// {
// FacebookSession::setDefaultApplication($this->appId, $this->appSecret);
// return new FacebookSession($this->pageToken);
// }
public function getRatings()
{
$fbRequest = $this->request('/766764863364715/ratings');
if($fbRequest != false) return $fbRequest->getGraphEdge();
return false;
// return $this->request('/766764863364715/ratings');
}
public function getProfilePicById($id)
{
$fbRequest = $this->request('/' . $id . '/picture?redirect=false');
if($fbRequest != false) return $fbRequest->getGraphObject();
return false;
//return $this->request('/' . $id . '/picture?redirect=false');
}
private function request($url)
{
try
{
$response = $this->facebook->get($url);
}
catch (FacebookResponseException $e){
// When Graph returns an error
//echo 'Graph returned an error: ' . $e->getMessage();
//exit;
return false;
} catch(FacebookSDKException $e) {
// When validation fails or other local issues
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
//exit;
return false;
}
return $response;
}
/*
|--------------------------------------------------------------------------
| Get Page Token: 1 The OAuth Dialog
|--------------------------------------------------------------------------
|
| First, register a Facebook application and obtain an application Id and secret.
| Now, within your website, create with a page that can only be accessed by your
| site’s admins. This page will provide a button to invoke the OAuth Dialog
| popup which will allow your admins to log into Facebook using your FB app.
|
*/
public function ShowOAuthDialog()
{
return '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $this->appId . '&redirect_uri=' . $this->redirectUrl . '&scope=manage_pages&state=code">Login</a>';
}
public function getPageToken()
{
// Retrieve Short Lived Token
$result = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id=' . $this->appId . '&client_secret=' . $this->appSecret . '&code=' . \Input::get('code'). '&redirect_uri=' . $this->redirectUrl);
$shortTokenData = [];
parse_str($result,$shortTokenData);
// Use Short Lived Token to retrieve Long Lived Token
$result = file_get_contents('https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=' . $this->appId . '&client_secret=' . $this->appSecret . '&fb_exchange_token=' . $shortTokenData['access_token']);
$longTokenData = [];
parse_str($result,$longTokenData);
// Use Long Lived Token to retrieve page data from the which the user is an admin
$result = file_get_contents('https://graph.facebook.com/me/accounts?access_token=' . $longTokenData['access_token']);
$json = json_decode($result);
// Page data
echo '<pre>';
dd($json) ;
}
}