Facebook PHP API post to wall falls under “Recent Posts by Others”

淺唱寂寞╮ 提交于 2019-11-30 12:17:18

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>';

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!