1

I have been looking like crazy for a solution to this problem but have not found what may be wrong. What I want to do is share my articles available on my website to all my Facebook pages.

To share articles on my private Facebook Wall is no proble. It works with the code below "// posting to profile"

require 'facebook.php';

    $facebook = new Facebook(array(
        'appId' => 'App ID',
        'secret' => 'Secret Code'
    ));


    if($facebook->getUser() == 0){
        $loginUrl = $facebook->getLoginUrl(array(
            scope => 'manage_pages,publish_actions'
        ));

        echo "<a href = '$loginUrl'>Login with facebook</a>";
    }
    else{

        //posting to pages

        $pages = $facebook->api('me/accounts');
        $id = $pages[data][0][id];
        $token = $pages[data][0][access_token];
        $api = $facebook->api($id . '', 'POST', array(
            access_token => $token,
            link => 'http://viralprinsen.se/post/se-arets-basta-overraskning-sa-avslojar-mannen-att-frun-ar-gravid',
            message => 'Se årets bästa överraskning: Så avslöjar mannen att frun är gravid.'
        ));

        //posting to profile
        $api = $facebook->api('me/feed', 'POST', array(
            link => 'http://viralprinsen.se/post/se-arets-basta-overraskning-sa-avslojar-mannen-att-frun-ar-gravid',
            message => 'Se årets bästa överraskning: Så avslöjar mannen att frun är gravid.'
        ));

        //displaying logout link
        echo

 "<br><a href = 'logout.php'>Logout</a>";
}
Albin Ferm
  • 93
  • 1
  • 1
  • 7
  • What exactly is your problem? – Brody Aug 14 '15 at 01:29
  • Nothing hapens, and i am really bad at coding so its so hard for me to "troubleshoot", i get no errors what so ever.. – Albin Ferm Aug 14 '15 at 01:33
  • Your question seems to imply that the code you have posted works. And it appears that you want to share to all your facebook pages (implying more than one account). Is this correct? – Brody Aug 14 '15 at 02:57
  • It works when i try to post something to my own Facebook wall, but not when i try to post to my Facebook pages, thats my problem. Right now im trying to just print out a list of all my facebook pages, but i can not get it to work.. :( – Albin Ferm Aug 14 '15 at 03:00
  • I'm not familiar with the PHP-Facebook-API but I think that you have to replace `$api = $facebook->api($id . '', 'POST', array(` with `$api = $facebook->api($id . '/feed', 'POST', array(` to access the feed of your page. – mario.klump Aug 14 '15 at 05:23
  • And you should use quotes when accessing your array: `$pages['data'][0]['id'];` instead of `$pages[data][0][id];` – mario.klump Aug 14 '15 at 05:26

1 Answers1

0

You are POSTing to the wrong endpoint:

$facebook->api($id . '', 'POST', array(...));

You should be posting to the /feed edge:

$facebook->api($id . '/feed', 'POST', array(...));

I would recommend that you check the latest SDK examples, wrap calls in try/catch and display all errors ... etc (see this).

Moreover, I would use the Graph API Explorer to understand the response structure of the Facebook Platform for each specific endpoint you want to work this (e.g. /me/accounts ... etc)

Community
  • 1
  • 1
ifaour
  • 37,558
  • 12
  • 70
  • 79