0

I am currently working on an app that connects high school students with nonprofits to help manage their social media pages and gain community service hours.

We give each student a social impact score calculated by the amount of likes, comments, shares that are garnered by their posts for the foundations. We are trying to create a server that pulls data from these Facebook pages with a never-expiring Page Access Token. Does this require a login script if we try to retrieve data on an ongoing basis? Also, our code below keeps showing an error.

What is wrong with it?

<?php
    // Defining FB SDK with absolute paths
    define('FACEBOOK_SDK_V4_SRC_DIR', 'http://www.website.com/src/Facebook/');
    require 'http://www.website.com/src/Facebook/autoload.php';

    use Facebook\FacebookSession;
    use Facebook\FacebookRequest;
    use Facebook\GraphUser;
    use Facebook\FacebookRequestException;

    FacebookSession::setDefaultApplication(‘App ID’,’App Secret’);

    $session = new FacebookSession(‘Never expiring page access token’);

    try {
        $data = (new FacebookRequest(
                    $session, 'GET', '/1503911716546178/posts'
                ))->execute()->getGraphObject()->getPropertyAsArray("data");

        foreach ($data as $post){
            $postId = $post->getProperty('id');
            $postMessage = $post->getProperty('message');
            print "$postId - $postMessage <br />";
        }
    } catch (FacebookRequestException $e) {
    // The Graph API returned an error
    } catch (\Exception $e) {
    // Some other error occurred
    }
?>
luschn
  • 68,448
  • 8
  • 104
  • 118
Rahul Chabria
  • 37
  • 1
  • 3

1 Answers1

0

Of course you need a login script to get a Page Token, but that would only be a one time thing. After getting the Extended Page Token, you can store it in a database and use it without user interaction.

More information about Tokens and how to generate them:

I suggest using the JavaScript SDK for login, it´s a lot easier to handle than the PHP SDK and the usability is better (no redirection). For example: http://www.devils-heaven.com/facebook-javascript-sdk-login/

About your code: hard to say without the actual error message, but there is no App ID or Secret, and there is no Token (DO NOT POST YOUR SECRET OR ANY TOKEN ON STACKOVERFLOW). Make sure you are using a valid Token, you can test it in the debugger: https://developers.facebook.com/tools/debug/

luschn
  • 68,448
  • 8
  • 104
  • 118
  • Thanks! We have already generated the token and we've used the applicable tokens in our code (hidden in our post for security) but when we load the page we just get a blank page on safari. (500 server error) – Rahul Chabria Jan 27 '16 at 14:49
  • that´s a typical php error, you have to debug your code – luschn Jan 27 '16 at 15:01
  • Thanks but we have debugged the code and its still showing the error. What we want to make sure is that none of the Facebook api code is wrong/erroneous, which maybe causing the error – Rahul Chabria Jan 27 '16 at 15:10
  • activate error logging in php, or you will not see anything else than your 500 server error – luschn Jan 27 '16 at 15:14
  • Thanks man. We are trying it now. – Rahul Chabria Jan 27 '16 at 15:36