如何使用 php 将网站或博客的帖子嵌入 Facebook 页面
How to Embed Posts from Website or Blog into Facebook Page using php
我正在开发一个模块,其中的要求是当管理员在网站或博客上创建任何新的 post 时,它也应该在其相关的 Facebook 页面上获得 post。
我想要这样的东西。但它在我的情况下不起作用。
这是代码
// 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
有关更多信息,请参阅此 link => 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
我正在开发一个模块,其中的要求是当管理员在网站或博客上创建任何新的 post 时,它也应该在其相关的 Facebook 页面上获得 post。
我想要这样的东西。但它在我的情况下不起作用。 这是代码
// 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
有关更多信息,请参阅此 link => 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