Facebook PHP API post to wall落在“他人最近的帖子”之下 [英] Facebook PHP API post to wall falls under "Recent Posts by Others"

查看:117
本文介绍了Facebook PHP API post to wall落在“他人最近的帖子”之下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

上面的代码发布到我的网页墙上,帖子是从页面喜欢我会从Facebook手动执行。这是非常有用的。

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.

但是,当我尝试添加更多字段,如link或picture或description在最近的帖子由其他人在TEST Jojo页面和该职位现在由我的个人账户(Joelle Landrie),而不是从页面本身。请参阅下面的代码。

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

请参阅: 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.





感谢Shadowfax谁问我是否正在使用page_access_token。我不是。我开始在网上查看如何获取此令牌,将其添加到我的代码中,现在它的效果非常好!

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!!

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

    }


推荐答案

答案为答案。

当作为页面发布时,您需要获得 manage_pages 权限,然后获得所需的页面的 access_token 通过 / me / accounts API调用,并使用令牌使 / {page_id} / feed POST呼叫。

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

    }

这篇关于Facebook PHP API post to wall落在“他人最近的帖子”之下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆