facebookfacebook-graph-apifacebook-php-sdkapp-secret

How to get images from FB to website


I'm working on publishing posts from our FB page on our website. Each new post and photos we added into our FB page I need to show in our website too.

I have this code, but don't working as I need.

require 'php-graph-sdk-5.x/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
    'app_id' => '...',
    'app_secret' => '{app-secret}', // don't know where to find app_secret for my page, I've found that just for apps, not pages
    'default_graph_version' => 'v2.10'
]);

$access_token = '...'; // generated in developers, but with just for 12 hours, than this token is expired... :-(

try {
    // Returns a `Facebook\FacebookResponse` object
    $response = $fb->get('me?fields=id,name,feed,data', $access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

$graphNode = $response->getGraphNode();

foreach ($graphNode as $node) {
    foreach ($node as $items) {
        print_r($items);

        // when access token was valid, I got posts from out FB page. But with no images, didn't find how to get add images to this feed...
    }
}

So, my questions are:

  1. where I can find app-secret for FB page? I'm not able to connect to my FB page without that, I think

  2. how to generate permanent access token, or if it's not possible, how to make i other way?

  3. how to add images (new, shared, etc.) to my feed?

Thanks.


Solution

    1. There is no App Secret per Page, there is only the App Secret per App. You can find it in your App Settings.
    2. Extended/Permanent Tokens: https://developers.facebook.com/docs/facebook-login/access-tokens/ or http://www.devils-heaven.com/facebook-access-tokens/ - for getting the feed entries of unrestricted Page, you can just use an App Access Token and you can hardcode it in your PHP code.
    3. Getting images in the feed, not just messages: You need to specify which fields you want to get, or you will only get default ones. It is called "Declarative Fields": https://developers.facebook.com/docs/graph-api/changelog#v2_4 - also, check out the API reference: https://developers.facebook.com/docs/graph-api/reference/post