使用PHP和SDK 5.0在Facebook上发布,就像页面一样

时间:2016-05-16 17:46:53

标签: php facebook api sdk

我正试图在Facebook页面上发布我是管理员的PHP代码。问题是,当我在我的个人网页中添加帖子时,我想将其发布到Facebook的公司页面中。这是有效的,我得到了帖子ID,但是,当我去Facebook时,我看到它像是“pepito与之分享链接”的通知,但它不在传记墙中。 )

这是我设置'manage_pages','publish_actions','pages_manage_instant_articles'的权限。

这是我的代码:

<?php
unset($_SESSION['facebook_access_token']);
$fbok = false;
session_start();
define('FACEBOOK_SDK_V5_SRC_DIR', 'facebook-sdk-v5/');
require_once 'facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
  'app_id' => 'XXXXXXXXX',
  'app_secret' => 'XXXXXXXXXX',
  'default_graph_version' => 'v2.6',
]);
if(!isset($_GET['error'])){
    if(isset($_SESSION['facebook_access_token'])){
        try {
            $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
            $requestUserName = $fb->request('GET', '/inmoejemplognu?fields=id,name');
            $batch = [ 'user-profile' => $requestUserName ];
            $responses = $fb->sendBatchRequest($batch);
            //$responses = json_decode($response->getBody());
            $data = [];
            $id = "";
            foreach ($responses as $key => $response) {
              if (!$response->isError()) {
                $data = json_decode($response->getBody());
                if($data->name == "Alquiler Vivienda"){
                    $id = $data->id;
                }       
              }
            }
            echo $id;
            $fbok = true;
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
            $helper = $fb->getRedirectLoginHelper();
            $permissions = ['manage_pages', 'publish_actions', 'pages_manage_instant_articles']; // optional
            $callback    = 'http://pablognu.tigrimigri.com/login-callback.php';
            $loginUrl    = $helper->getLoginUrl($callback, $permissions);
            header('Location: '. $loginUrl);
        }
    } else {
        $helper = $fb->getRedirectLoginHelper();
        $permissions = ['manage_pages', 'publish_actions', 'pages_manage_instant_articles']; // optional
        $callback    = 'http://pablognu.tigrimigri.com/login-callback.php';
        $loginUrl    = $helper->getLoginUrl($callback, $permissions);
        header('Location: '. $loginUrl);
    }
}
if($fbok){
    echo "Podemos publicar";
            $linkData = [
              'link' => 'http://www.google.es',
              'message' => 'Un ejemplo de publicacion',
             ];
            try {
              $response = $fb->post('/'.$id.'/feed', $linkData, $_SESSION['facebook_access_token']);
            } catch(Facebook\Exceptions\FacebookResponseException $e) {
                echo 'Graph returned an error: ' . $e->getMessage();
              unset($_SESSION['facebook_access_token']);
              exit;
            } catch(Facebook\Exceptions\FacebookSDKException $e) {
              echo 'Facebook SDK returned an error: ' . $e->getMessage();
              exit;
            }
            $graphNode = $response->getGraphNode();
            echo 'Posted with id: ' . $graphNode['id'];
//} else {
//  echo "No se ha autorizado el acceso para publicar en facebook";
}

&GT;

全部谢谢

1 个答案:

答案 0 :(得分:0)

如果你想发布“as Page”(这是你想要实现的,我假设),你必须使用publish_pages,而不是publish_actions。而且你必须使用页面令牌。文档非常具体:https://developers.facebook.com/docs/graph-api/reference/v2.6/page/feed#publish

关于访问令牌: