如何使用php

时间:2017-08-14 13:07:14

标签: php facebook facebook-php-sdk

我正在开发一个模块,其中要求是当管理员在网站或博客上创建任何新帖子时,它也应该在其相关的Facebook页面上发布。

我想要这样的东西。但它不适合我的情况。 这是代码

// require Facebook PHP SDK
// see: https://developers.facebook.com/docs/php/gettingstarted/
require_once("php-graph-sdk-5.5/src/Facebook/facebook.php");
require_once("php-graph-sdk-5.5/src/Facebook/autoload.php");

// initialize Facebook class using your own Facebook App credentials
// see: https://developers.facebook.com/docs/php/gettingstarted/#install
$config = array();
$config['app_id'] = 'myappud';
$config['app_secret'] = 'myappsecret';
$config['fileUpload'] = false; // optional

$fb = new \Facebook\Facebook($config);

// define your POST parameters (replace with your own values)
$params = array(
    "access_token" => "myaccesstoek", // see: https://developers.facebook.com/docs/facebook-login/access-tokens/
    "message" => "Here is a blog post about auto posting on Facebook using PHP #php #facebook",
    "link" => "http://www.pontikis.net/blog/auto_post_on_facebook_with_php",
    "picture" => "http://i.imgur.com/lHkOsiH.png",
    "name" => "How to Auto Post on Facebook with PHP",
    "caption" => "www.pontikis.net",
    "description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation."
);

// post to Facebook
// see: https://developers.facebook.com/docs/reference/php/facebook-api/
try {
    $ret = $fb->post('myfburl/feed', $params);
    echo 'Successfully posted to Facebook';
}
catch(Exception $e) {
    echo $e->getMessage();
}

它给出错误

An unknown error occurred

有关详情,请参阅此链接=> http://www.pontikis.net/blog/auto_post_on_facebook_with_php

帮助弄清楚我犯错的地方。

已更新

    <?php

// Include facebook class (make sure your path is correct)
use Facebook\Facebook; 

require_once ("php-graph-sdk-5.5/src/Facebook/autoload.php");

require_once("php-graph-sdk-5.5/src/Facebook/Facebook.php");

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'app_id'  => 'myappid',
  'app_secret' => 'myappsecret',
  'cookie' => true,
));

//$token is the access token from the URL above
$post = array('access_token' =>'myaccesstoken', 'message' => 'new test post - ' . date('Y-m-d'));

try{
$res = $facebook->post('myfburl/feed',$post);
print_r($res);

} catch (Exception $e){

    echo $e->getMessage();
}

1 个答案:

答案 0 :(得分:0)

要发布到网页,您需要具有manage_pages和publish_pages权限的网页令牌

示例

https://www.facebook.com/dialog/oauth?client_id=yourClientId&redirect_uri=yourUri/&scope=manage_pages,publish_pages