11

我想在 Facebook 主页的墙上发布消息。我是该应用程序和此代码中使用的页面的管理员,我已经授予了我的应用程序能够在我的页面上发布所需的权限,当我仅使用“消息”字段时它可以工作,如下所示:

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

上面的代码发布到我的页面墙上,并且帖子是“从”页面本身制作的,就像我从 facebook 手动执行一样。这很好用。

但是,当我尝试添加“链接”、“图片”或“描述”等更多字段时,该帖子会出现在“其他人在 TEST Jojo 页面上发布的最新帖子”中,而该帖子现在是由我的个人帐户 (Joelle Landrie) 制作的来自页面本身。请参阅下面的代码。

$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);

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

link字段似乎引起了问题,我可以使用message,picturedescription字段在我的页面上成功发布帖子。只有这对我没用,我需要我的帖子有链接。



解决方案

感谢 Shadowfax 询问我是否使用“page_access_token”。我不是。我开始在网上寻找如何获得这个令牌,将它添加到我的代码中,现在它工作得很好!!

最终代码

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

    }
4

1 回答 1

2

只需将答案发布为答案。

作为页面发布时,您需要获得许可,然后通过API 调用manage_pages获取所需页面并使用令牌进行POST 调用。access_token/me/accounts/{page_id}/feed

最初的发帖者 Flames 设法做到了这一点,并在问题本身中发布了他的解决方案。我只是将它粘贴在这里并制作它社区维基

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

    }
于 2013-10-25T01:37:58.643 回答