Home Facebook GRAPH and PHP SDK via Laravel
Reply: 0

Facebook GRAPH and PHP SDK via Laravel

Alexey Khachatryan
1#
Alexey Khachatryan Published in 2018-01-09 14:54:27Z

I have a Laravel project and I installed Facebook PHP SDK on it. I created a page, where user need to fill facebook page ID and the system will show some data. It worked good with public data, such as posts, comments, likes, shares. After I started to work on private data - insights. I'm working on Facebook API Explorer and in my project. I tried everything to get the data, but I always get an error.

For example Facebook API Explorer I did all as mentioned here https://stackoverflow.com/a/33965743/8913631

I spent 3 days trying to find the reason, but false.

I have a facebook api controller and in this controller I have a __contruct function

  /**
 * FB obj init, getting access token
 * @param Request $request
 */
public function __construct(Request $request)
{

    //If it is not POST - show FB page
    if ($request->isMethod('post')) {
        //Connecting
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => 'https://graph.facebook.com/oauth/access_token?client_id=' . env("FACEBOOK_ID") . '&client_secret=' . env("FACEBOOK_SECRET") . '&grant_type=client_credentials'
        ));
        $this->access_token = json_decode(curl_exec($curl))->access_token;
        curl_close($curl);


        //FB object Init
        $this->fb = new Facebook([
            'app_id' => env("FACEBOOK_ID"),
            'app_secret' => env("FACEBOOK_SECRET"),
            'default_graph_version' => 'v2.10',
            'default_access_token' => $this->access_token
        ]);

        //Page ID
        $this->page_id = $request->input('page_id');

    }
}

When I got the access token, I do request from another function

 public function getPageInfo(){
    $this->fb_user_data = session('fb_data');

    $data = $this->getData('/'. $this->page_id .'/insights?metric=page_impressions_unique', true);

And this is the function, which sends request to FB server

 /**
 * getData
 *
 * Sending request to Facebook and handling the response
 *
 * @param string $query
 *
 * @param boolean $personalInformation
 * If this parameter is true the function will use special Page access token for getting private information
 * else - only public, such as comments, posts etc
 *
 * @return GraphNode $response
 */
protected function getData($query, $personalInformation = false){
    try {
        // Get the \Facebook\GraphNodes\GraphUser object for the current user.
        // If you provided a 'default_access_token', the '{access-token}' is optional.
        if($personalInformation)
            $response = $this->fb->get( $query, '******' );
        else
            $response = $this->fb->get( $query );
    } 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;
    }


    return $response;

}

Also I connected to my project OAuth classes and I made auth with Facebook. After auth I have data like this

object(Laravel\Socialite\Two\User)#317 (11) {
  ["token"]=>
  string(182) "***************************"
  ["refreshToken"]=>
  NULL
  ["expiresIn"]=>
  int(5169090)
  ["id"]=>
  string(16) "***"
  ["nickname"]=>
  NULL
  ["name"]=>
  string(31) "****"
  ["email"]=>
  string(18) "a***u"
  ["avatar"]=>
  string(69) "https://graph.facebook.com/v2.10/***/picture?type=normal"
  ["user"]=>
  array(6) {
    ["name"]=>
    string(31) "*****"
    ["email"]=>
    string(18) "al***@***u"
    ["gender"]=>
    string(4) "male"
    ["verified"]=>
    bool(true)
    ["link"]=>
    string(61) "https://www.facebook.com/app_scoped_user_id/***/"
    ["id"]=>
    string(16) "***"
  }
  ["avatar_original"]=>
  string(68) "https://graph.facebook.com/v2.10/***/picture?width=1920"
  ["profileUrl"]=>
  string(61) "https://www.facebook.com/app_scoped_user_id/***/"
}

Who can tell me, where is my mistake? How and what should I do to get the right access token for private page? I tried everything. What is the algorithm? Thanks

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309189 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO