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

时间:2010-10-29 19:55:56

标签: php api facebook

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

View 1

但我得到的只是如下图所示。此外,它仅对此配置文件可见(不是对喜欢/ etc的朋友/ ppl)。

View 2

这是我目前的代码

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;

    }

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

3 个答案:

答案 0 :(得分:20)

您需要确保为用户请求'manage_pages'权限。完成后,您可以执行$facebook->api('/me/accounts'),然后您将收到一个令牌(以及页面信息),您可以使用该令牌在页面页面上发布。< / p>

答案 1 :(得分:0)

我在这一天的大部分时间都在努力,然后发现不使用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());
        }

    }

答案 2 :(得分:-1)

/* Note: The default value of the `opaque' property for CAMetalLayer
 * instances is true. */