使用Facebook Graph API v3.1 PHP SDK在Facebook个人资料/页面时间轴上发布

问题描述:

您知道Facebook在2018年7月26日推出了新的Graph API v3.1,这为开发人员带来了许多变化.我现在的问题之一是如何使用Facebook Graph API v3.1 PHP SDK在Facebook个人资料/页面时间线上共享/发布?

As you know that Facebook introduced new Graph API v3.1 on July 26, 2018 that changed many things for developers. One of the questions I have now is how to share/post on Facebook profile/page timeline using Facebook Graph API v3.1 PHP SDK?

提醒:Graph API v2.7将在2018年10月5日弃用.请使用 API升级工具,以了解这可能对您的应用有何影响.有关更多详细信息,请参见更改日志

Reminder: Graph API v2.7 will be deprecated on Oct 05, 2018. Please use the API Upgrade Tool to understand how this might impact your app. For more details see the changelog

为此,我创建了一个具有某些设置的新应用,如以下屏幕截图所示.

For this I created a new apps with some settings as shown in the below screenshots.

为此,我使用了下面提到的代码以及 facebook-php-graph-sdk -5.x .

For this purpose, I used the below mentioned code along with facebook-php-graph-sdk-5.x.

<?php
if(!session_id()){
    session_start();
}

// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-graph-sdk-5.x/autoload.php';

// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;

/*
 * Configuration and setup Facebook SDK
 */
$appId         = 'APP_ID'; //Facebook App ID
$appSecret     = 'APP_SECRET'; //Facebook App Secret
$redirectURL   = 'MAIN_PAGE_URL_SAME_AS_IN_APPS_SETTING'; //Callback URL
$fbPermissions = array('publish_actions'); //Facebook permission

$fb = new Facebook(array(
    'app_id' => $appId,
    'app_secret' => $appSecret,
    'default_graph_version' => 'v2.6',
));

// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();

// Try to get access token
try {
    if(isset($_SESSION['facebook_access_token'])){
        $accessToken = $_SESSION['facebook_access_token'];
    }else{
        $accessToken = $helper->getAccessToken();
    }
} catch(FacebookResponseException $e) {
     echo 'Graph returned an error: ' . $e->getMessage();
      exit;
} catch(FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
}
?>

index.php

<?php
// Include FB configuration file
require_once 'fbConfig.php';

if(isset($accessToken)){
    if(isset($_SESSION['facebook_access_token'])){
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }else{
        // Put short-lived access token in session
        $_SESSION['facebook_access_token'] = (string) $accessToken;

        // OAuth 2.0 client handler helps to manage access tokens
        $oAuth2Client = $fb->getOAuth2Client();

        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;

        // Set default access token to be used in script
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }

    //FB post content
    $message = 'Test message from *.com website';
    $title = 'Post From Website';
    $link = 'http://www.*.com/';
    $description = '* is simply awesome.';
    $picture = 'https://i.stack.imgur.com/MybMA.png';

    $attachment = array(
        'message' => $message,
        'name' => $title,
        'link' => $link,
        'description' => $description,
        'picture'=>$picture,
    );

    try{
        // Post to Facebook
        $fb->post('/me/feed', $attachment, $accessToken);

        // Display post submission status
        echo 'The post was published successfully to the Facebook timeline.';
    }catch(FacebookResponseException $e){
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    }catch(FacebookSDKException $e){
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
}else{
    // Get Facebook login URL
    $fbLoginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);

    // Redirect to Facebook login page
    echo '<a href="'.$fbLoginURL.'"><img src="https://www.freeiconspng.com/uploads/facebook-login-button-png-11.png" /></a>';
}

?>

我的文件级别如以下屏幕快照所示.

Where as my files level are as shown in the below screenshot.

最后,在完成所有上述设置和代码后,当我尝试运行页面时,我得到了登录脸部按钮,然后单击此处,则出现以下屏幕截图错误.

Finally after doing all of the above setting and codes, when I try to run my page then I got LOGIN FACEBOOK BUTTON and after clicking there, I got the below screenshot error.

我想要的是简单地发布我想要的内容,而不显示任何POPUP或对话框,以便我可以通过我的PHP代码轻松使用它.

What I want is simple post my desired content without showing any POPUP or dialog so that I can easily use it via my PHP Codes.

我试图在Internet上找到任何可行的解决方案,但现在它们都已过时,现在无法与新的Facebook Graph API v3.1一起使用.

I tried to find any working solution over internet but all are now old, nothing is now working with new Facebook Graph API v3.1.

我想要的是简单地发布我想要的内容,而不显示任何POPUP或对话框

What I want is simple post my desired content without showing any POPUP or dialog

对于用户个人资料,这不再是可能.您必须改为使用共享对话框". publish_actions被删除,没有替代品或解决方法.

That is not possible anymore for user profiles. You have to use Sharing Dialogs instead. publish_actions got removed and there is no replacement or workaround.

对于Pages,您可以使用manage_pagespublish_pages将具有页面令牌的页面发布到页面上.

For Pages, you can use manage_pages and publish_pages to post to a Page with a Page Token.