HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/sportivo.komma.pro/app/Komma/References/FacebookService.php
<?php


namespace Komma\References;

use Facebook\FacebookRequest;
use Facebook\FacebookSession;

class FacebookService
{
    private $appId = '524670841034785';
    private $appSecret = '0de7ca73f4e9fd9b33cf27be66b056c8';
    private $pageToken = 'CAAHdL3MamCEBAI8aaDmMs5OlMJy56mc1gg2H1TpilB41KZBF4Qmc8Nr4gaHvrfDJ5gZAbzFNB2BZCzZAf78OGEetQHBUf6ZCsZAcn8H6ugYix63pvuhamF69BA80cGoyMkaEn7SXiKxggRBQ2OD7pSF3ZBIVZAIGGcdM3SAIJ7IvgojNnNCqg8Oo';
    private $redirectUrl = 'http://sportivo.komma.pro/facebook';

    /*
     * 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()
    {
        return $this->request('/sportivo.budel/ratings');
    }

    public function getProfilePicById($id)
    {
        return $this->request('/' . $id . '/picture?redirect=false');
    }

    private function request($url)
    {
        try
        {
            $request = new FacebookRequest($this->session(),'GET',$url);
            $response = $request->execute();
        }
        catch(\Exception $e)
        {
            return false;
        }
        return $response->getGraphObject();
    }




    /*
    |--------------------------------------------------------------------------
    | 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) ;
    }
}