Facebook PHP SDK V5和AJAX Graph API页面请求

问题描述:

I'm trying without success for 2 days now to retrieve page feed with Facebook PHP SDK. Details :

  • I got a user, who is page admin
  • I created an app (not public, is that a problem?)
  • I use an AJAX called PHP script to try to retrieve feed, because I don't want to pollute main page loading.

All stuff given on PHP SDK doc is related to manual login and access token retrieval. I Managed to retrieve a user token using the 2-scripts code (login, and callback), with manage_pages speical grant require :

$fb = new Facebook\Facebook([
   'app_id' => $app_id,
   'app_secret' => $app_secret,
   'default_graph_version' => 'v2.2',
]);

$helper = $fb->getRedirectLoginHelper();

$permissions = ['manage_pages']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://domain.com/fb_callback.php',       $permissions);

How can achieve the same thing without manual redirection, is one script, in AJAX context? SHould I use Curl to emulate manual redirection?

THing which puzzle me out is that the same stuff tok 2 lines using JS framework :

window.fbAsyncInit = function() {
    // Framework init with Vinexpo app ID
    FB.init({
        appId      : '012345789',
        xfbml      : true,
        version    : 'v2.4'
    });

    // Check status
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {

            // If connected, get last 2 posts on Facebook page
            FB.api(
                "/page_ID/posts?limit=2",
                {
                    access_token : response.authResponse.accessToken
                },
                function (responsePost) {
                    if (responsePost && !responsePost.error) {
                        // Fill tiles on social network panel

                    }
                }
            );
        }
    });

Thanks in advance for answers or leads.

If you just want to read the newsfeed of one of you pages via AJAX, you don't necessarily need the PHP SDK if you generate yourself an eternal page access token and use this for your plain HTTP requests.

Have a look at my answer here on how to get such a token: