Facebook OpenGraph 3.0使用PHP发布到FB页面

时间:2018-05-07 03:19:09

标签: php facebook facebook-graph-api

我不确定我哪里出错,但需要从我的网站发布到我的Facebook页面。

~fbautopost.php

require_once("Facebook/facebook.php");

class FacebookPost 
{

var $consumer;
var $token;
var $method;
var $http_status;
var $last_api_call;
var $callback;
var $connection;
var $access_token;

function __construct($data)
{
    $config = array();
    $config['appId'] = $data['consumer_key'];
    $config['secret'] = $data['consumer_secret'];
    $pageID = $data['page_ID'];

    $this->connection = new Facebook($config);
}

function share($title, $targetUrl, $imgUrl, $description, $access_token)
{
    $this->connection->setAccessToken($access_token);
    $params["access_token"] = $access_token;

    if(!empty($title))
    {
        $params["message"] = $title;
        $params["name"] = $title;
    }

    if(!empty($targetUrl))
    {
        $params["link"] = $targetUrl;
    }

    if(!empty($imgUrl))
    {
        $params["picture"] = $imgUrl;
    }

    if(!empty($description))
    {
        $params["description"] = $description;
    }

    // post to Facebook
    try 
    {
        $ret = $this->connection->api('/' .$pageID . '/feed/', 'post', $params);
    }
    catch(Exception $e) 
    {
        $e->getMessage();
    }

    return true;
}

function getLoginUrl($params)
{
    return $this->connection->getLoginUrl($params);
}

function getContent($url) 
{
    $ci = curl_init();

    /* Curl settings */
    curl_setopt($ci, CURLOPT_URL, $url);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ci, CURLOPT_HEADER, false);
    curl_setopt( $ci, CURLOPT_CONNECTTIMEOUT, 10 );
    curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($ci);
    curl_close ($ci);
    return $response;
}
}

~post.php

include('fbautopost.php');

$access_token = 'MY_ACCESS_TOKEN';
$facebookData = array();
$facebookData['consumer_key'] = 'MY_APP_ID';
$facebookData['consumer_secret'] = 'MY_SECRET_KEY';
$facebookData['page_ID'] = 'MY_PAGE_ID';

$title = 'Post Title';
$description = 'This is a test post'; 

$facebook = new FacebookPost($facebookData);
$facebook->share($title, $description, $access_token);

我还没有将应用程序从开发中删除,但作为管理员,我没有在页面上看到任何测试帖子。

我可能没有得到正确的访问权限?在Graph Explorer中,我选择了我的应用程序,然后获得具有manage_page和publish_page权限的用户令牌。我也尝试过使用页面令牌。都没有工作。我的代码是坏的还是我错过了其他的东西?

TIA

1 个答案:

答案 0 :(得分:0)

问题在于我需要require_once(' Facebook / autoload.php'),而不是我拥有的Facebook.php。