如何从网站或博客嵌入帖子到Facebook页面使用php
问题描述:
我正在一个模块,其中要求是当管理员在网站或博客上创建任何新帖子,那么它也应该在其相关的Facebook页面上发布。如何从网站或博客嵌入帖子到Facebook页面使用php
我想要这样的东西。但它不适用于我的情况。 这里是代码
// 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();
}
答
张贴到你需要与manage_pages页令牌的页面和publish_pages权限
例:
https://www.facebook.com/dialog/oauth?client_id=yourClientId&redirect_uri=yourUri/&scope=manage_pages,publish_pages
首先,您不能在共享时动态指定图片,名称,标题和说明, e https://developers.facebook.com/docs/apps/changelog#v2_9_90_day_deprecations – CBroe
我删除,但仍然是相同的错误。 @CBroe –
你正在使用什么类型的访问令牌,它包含什么权限? – CBroe