Facebook PHP API post to wall falls under “Recent

2019-02-08 18:21发布

问题:

I want to post a message to the wall of a Facebook Page. I am the admin of the app and the page used in this code, I already gave permissions needed for my app to be able to post on my page, it works when I use only the field "message", like this:

$message = array(
            'message' => "Test2",<br>
           );
$result = $fb->api('/411895472189524/feed','POST',$message);

The code above posts to my page wall and the post is made "from" the page itself, just like if I would do it manually from facebook. This is working great.

But when I try to add more fields like "link" or "picture" or "description" the post goes in the "Recent Posts by Others on TEST Jojo Page" and the post is now made from my personnal account (Joelle Landrie) instead of from the page itself. See code below.

$message = array(
            'message' => "Test2",
            'picture' => "http://www.cleanpopo.com/uploads/1/3/1/5/13154615/245431315.jpg",
            'description' => "This is a test description",
            'link' => "google.com"
           );
$result = $fb->api('/411895472189524/feed','POST',$message);

See: https://www.facebook.com/pages/TEST-Jojo-Page/411895472189524

The link field seems to be causing problem, I can get a successful post on my page using the message, picture and description field. Only this is useless to me, I need my post to have a link.



SOLUTION

Thanks to Shadowfax who asked if I was using the "page_access_token". I was not. I started looking on the web how to get this token, added it to my code and now it works great!!

The Final Code

$appId = 'YOUR APP ID';
$secret = 'YOUR SECRET';
$returnurl = 'http://www.yoursite.com';
$permissions = 'manage_pages, publish_stream, offline_access';

$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();

if($fbuser){

        $page_id = "YOUR PAGE ID";
        $page_access_token = "";
        $result =  $fb->api("/me/accounts");

        // loop trough all your pages and find the right one
        if( !empty($result['data']) )
        {
           foreach($result["data"] as $page) 
           {
             if($page["id"] == $page_id)
             {
               $page_access_token = $page["access_token"];
               break;
             }
           }
        }
        else
        {
          echo "AN ERROR OCCURED: could not get the access_token. Please verify the page ID ".$page_id." exists.";
        }

        // set the facebook active facebook access token as the one we just fetch
        $fb->setAccessToken($page_access_token);

        // Now try to post on page's wall
        try{
            $message = array(
                'message' => "YOUR MESSAGE",
                'picture' => "YOUR PICTURE",
                'description' => "YOUR DESCRIPTION",
                'link' => "YOUR LINK"
            );
            $result = $fb->api('/'.$page_id.'/feed','POST',$message);
            if($result){
                echo 'Successfully posted to Facebook Wall...';
            }
        }catch(FacebookApiException $e){
            echo $e->getMessage();
        }

    }else{

        $fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
        echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';

    }

回答1:

Just posting the answer as answer.

When posting as a page, you need to get manage_pages permission, then get the desired page's access_token via /me/accounts API call and use that token to make the /{page_id}/feed POST call.

Flames, the original poster, managed to do this and posted his solution edited in the question itself. I just pasting it here and making it Community Wiki

$appId = 'YOUR APP ID';
$secret = 'YOUR SECRET';
$returnurl = 'http://www.yoursite.com';
$permissions = 'manage_pages, publish_stream, offline_access';

$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();

if($fbuser){

        $page_id = "YOUR PAGE ID";
        $page_access_token = "";
        $result =  $fb->api("/me/accounts");

        // loop trough all your pages and find the right one
        if( !empty($result['data']) )
        {
           foreach($result["data"] as $page) 
           {
             if($page["id"] == $page_id)
             {
               $page_access_token = $page["access_token"];
               break;
             }
           }
        }
        else
        {
          echo "AN ERROR OCCURED: could not get the access_token. Please verify the page ID ".$page_id." exists.";
        }

        // set the facebook active facebook access token as the one we just fetch
        $fb->setAccessToken($page_access_token);

        // Now try to post on page's wall
        try{
            $message = array(
                'message' => "YOUR MESSAGE",
                'picture' => "YOUR PICTURE",
                'description' => "YOUR DESCRIPTION",
                'link' => "YOUR LINK"
            );
            $result = $fb->api('/'.$page_id.'/feed','POST',$message);
            if($result){
                echo 'Successfully posted to Facebook Wall...';
            }
        }catch(FacebookApiException $e){
            echo $e->getMessage();
        }

    }else{

        $fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
        echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';

    }