Facebook Graph API PHP SDK 作为页面发布在页面上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4055440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:49:52  来源:igfitidea点击:

Facebook Graph API PHP SDK posting on page as page

phpapifacebook

提问by Misiur

It's final try with PHP, if it fails, I'll try with JS. So my goal is to post on FB page as "Page name" through PHP: this is what I want to get

这是 PHP 的最后一次尝试,如果失败,我会尝试使用 JS。所以我的目标是通过 PHP 在 FB 页面上以“页面名称”的形式发布:这就是我想要的

View 1

查看 1

But all I get is shown pic below. Also, it's visible ONLY to this profile (not to friends/ppl who like/etc.).

但我得到的只是下面的图片。此外,它仅对这个个人资料可见(对喜欢/等的朋友/人不可见)。

View 2

查看 2

This is my current code

这是我当前的代码

function post_facebook($data=null, $redir = null){
        $result = "";
        require_once (ROOT. "/apps/configuration/models/ConfigurationItem.php");
        require_once (ROOT . "/components/facebook/facebook.php");

        $this->ConfigurationItem = new ConfigurationItem($this->getContext());

        $row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_login');
        $apiid=$row['value']; <= Correct apiid

        $row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_pass');
        $secret=$row['value']; <= Correct secret key

        $facebook = new Facebook(array(
          'appId'  => $apiid,
          'secret' => $secret,
          'cookie' => true,
        ));

        $session = $facebook->getSession();
        $me = null;
        if ($session) {
            try {
                $uid = $facebook->getUser();
                $me = $facebook->api('/me');
            } catch (FacebookApiException $e) {
                error_log($e);
            }
            $message=$data['facebook_text'];
            $attachment = array(
                'message' => $data['facebook_text'],
                'name' => $data['name'],
                'link' => $this->getLinkToLatestNews(),
                'description' => '',
            );

            try {
                $facebook->api('/PAGE ID/feed/', 'post', $attachment);
                $result = "Facebook: Sent";
            } catch (FacebookApiException $e) {
                $result = "Facebook: Failed";
                error_log($e);
            }
        } else {
            $login_url = $facebook->getLoginUrl();
            header("Location: ".$login_url);
            exit;
        }

        echo $result;
        exit;
        //return $result;

    }

What I'm doing wrong? I couldn't find anything in API documentation/top google results, only for JS. Thanks for help!

我做错了什么?我在 API 文档/顶级谷歌结果中找不到任何内容,仅适用于 JS。感谢帮助!

回答by njorden

You'll need to make sure you're requesting the 'manage_pages' permission for the user. Once you've got that you can do $facebook->api('/me/accounts')and you'll receive a token back (along with the page info) that you can use to post on the page asthe page.

您需要确保为用户请求“manage_pages”权限。一旦你得到了,你可以做$facebook->api('/me/accounts'),你会收到一个令牌背部(与页面信息一起),你可以用它来发布页上页面。

回答by mutatron

I struggled with this most of the day, then found that not using setAccessToken(page_access_token) was the only thing preventing it from working for me. I found that in a stackoverflow post from 18 months ago. I'll put my solution here, for anyone who has this question in the future:

我一天中的大部分时间都在为此苦苦挣扎,然后发现不使用 setAccessToken(page_access_token) 是阻止它为我工作的唯一原因。我在 18 个月前的 stackoverflow 帖子中发现了这一点。我会把我的解决方案放在这里,供以后有这个问题的任何人使用:

protected $scope = "email,publish_stream,manage_pages";

    $url = "{$api_url}/{$fbusername}/accounts?access_token=".$access_token;
    $response = json_decode(file_get_contents($url));
    foreach($response->data as $data) {
        try
        {
            $res = $this->SDK->setAccessToken($data->access_token);
            $res = $this->SDK->api(
                "{$data->id}/feed",
                "POST",
                array('link' => 'www.example.com',
                      'message' => 'This is a test message from php',)
            );
            log::debug(__FUNCTION__, print_r($res,true));
        }
        catch (Exception $e)
        {
            log::debug(__FUNCTION__, $e->getType().": ".$e->getMessage());
        }

    }

回答by Sabrina Pati?o

$feed = '/v2.8/' . $pageID . '/' . "feed";
$params = array(
 "access_token" => AQUI TU TOKEN // see: https://developers.facebook.com/docs/facebook-login/access-tokens/
);
$params[ "link" ] = "https://zapatillasnewbalancebaratas.blogspot.com/2018/11/zapatilla-new-balance-ml515-col.html";
$params[ "message" ] = "Zapatilla New Balance Ml515 Col";
$params[ "method" ] = POST;

$graph_url = "https://graph.facebook.com" . $feed;


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $graph_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        $output = curl_exec($ch);
        echo $output;
        curl_close($ch);