0
我有一个网站,我需要它不时发布状态更新到Facebook页面。以编程方式登录Facebook并发布到页面
使用我的个人Facebook帐户,我创建了一个应用程序和一个页面。到目前为止,我已经能够以编程方式张贴到我的网页长城,通过将此代码添加到我的网站:
<?php
session_start();
require 'src/config.php';
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $config['App_ID'],
'secret' => $config['App_Secret'],
'cookie' => true
));
if(isset($_POST['status']))
{
$page = split("-",$_POST['page']);
$page_token = $page[0];
$page_id= $page[1];
// status with link
$publish = $facebook->api('/'.$page_id.'/feed', 'post',
array('access_token' => $page_token,
'message'=> $_POST['status'],
'from' => $config['App_ID'],
'to' => $page_id,
'caption' => 'Caption',
'name' => 'Name',
'link' => 'http://www.example.com/',
'picture' => 'http://www.phpgang.com/wp-content/themes/PHPGang_v2/img/logo.png',
'description' => $_POST['status'].' via demo.PHPGang.com'
));
//Simple status without link
//$publish = $facebook->api('/'.$page_id.'/feed', 'post',
// array('access_token' => $page_token,'message'=>$_POST['status'] .' via PHPGang.com Demo',
// 'from' => $config['App_ID']
// ));
echo 'Status updated.<br>';
$graph_url_pages = "https://graph.facebook.com/me/accounts?access_token=".$_SESSION['token'];
$pages = json_decode(file_get_contents($graph_url_pages)); // get all pages information from above url.
$dropdown = "";
for($i=0;$i<count($pages->data);$i++)
{
$dropdown .= "<option value='".$pages->data[$i]->access_token."-".$pages->data[$i]->id."'>".$pages->data[$i]->name."</option>";
}
echo '
<style>
#status
{
width: 357px;
height: 28px;
font-size: 15px;
}
</style>
'.$message.'
<form action="index.php" method="post">
Select Page on which you want to post status: <br><select name="page" id=status>'.$dropdown.'</select><br><br>
<input type="text" name="status" id="status" placeholder="Write a comment...." /><input type="submit" value="Post On My Page!" style="padding: 5px;" />
<form>';
}
elseif(isset($_GET['fbTrue']))
{
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=".$config['App_ID']."&redirect_uri=" . urlencode($config['callback_url'])
. "&client_secret=".$config['App_Secret']."&code=" . $_GET['code'];
$response = file_get_contents($token_url); // get access token from url
$params = null;
parse_str($response, $params);
$_SESSION['token'] = $params['access_token'];
$graph_url_pages = "https://graph.facebook.com/me/accounts?access_token=".$_SESSION['token'];
$pages = json_decode(file_get_contents($graph_url_pages)); // get all pages information from above url.
$dropdown = "";
for($i=0;$i<count($pages->data);$i++)
{
$dropdown .= "<option value='".$pages->data[$i]->access_token."-".$pages->data[$i]->id."'>".$pages->data[$i]->name."</option>";
}
echo '
<style>
#status
{
width: 357px;
height: 28px;
font-size: 15px;
}
</style>
'.$message.'
<form action="index.php" method="post">
Select Page on which you want to post status: <br><select name="page" id=status>'.$dropdown.'</select><br><br>
<input type="text" name="status" id="status" placeholder="Write a comment...." /><input type="submit" value="Post On My Page!" style="padding: 5px;" />
<form>';
}
else
{
echo 'Connect <a href="https://www.facebook.com/dialog/oauth?client_id='.$config['App_ID'].'&redirect_uri='.$config['callback_url'].'&scope=publish_pages,manage_pages"><img src="./images/login-button.png" alt="Sign in with Facebook"/></a>';
}
所以,我刚打开我的网站,点击“请登录”,登录我自己。一旦我登录后,它现在可以将状态更新发布到Facebook页面。
显然,这里的问题是我需要登录才能发布。如果其他用户将尝试使用其用户帐户登录,我的网站无法将状态更新发布到Facebook页面,因为我是该应用/页面的唯一管理员。
我的问题是,有没有办法让我以编程方式登录到Facebook,这样我可以自动执行这些状态更新到我的页面?
对不起,这里总共noob到Facebook的发展。
我在Stackoverflow here上发表了一篇文章。这和我的问题一样。但他们使用“OFFLINE_ACCESS”权限来使访问令牌终身有效。但Facebook删除了这个功能here。所以我现在怎么做?
请评论的downvote原因。我的答案是100%正确的,但如果有任何错误,我很乐意提高。 – luschn
我相信它没有回答这个问题,因为他表示自己与另一个Stack问题存在同样的问题,这个问题涉及自2012年以来贬值的'OFFLINE_ACCESS'。我与Aaron有同样的问题,所以如果您能够巩固这个。 – CommonKnowledge
那确切的问题是什么呢?我很高兴回答与该主题相关的其他问题,但我的回答包括关于如何获得扩展页面令牌(这是他需要在没有用户登录的情况下发布)以及一些其他链接的非常详细的描述。除此之外别无他法。他虽然使用了很老的php sdk,但他肯定应该升级。换句话说: – luschn