目前,我的应用每次访问应用时都会发布到用户墙。我只希望它在第一次授权应用时发布到墙上一次。然后,每次他们访问后,它只会更新新闻Feed状态。
这是我目前的代码:
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$access_token = $facebook->getAccessToken();
$vars = array(
'message' => "Message goes here",
'picture' => "image",
'link' => "link here",
'name' => "Name here",
'caption' => "Caption here"
);
$result = $facebook->api('/me/feed', 'post', $vars);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=> $app_url));
echo "<script type='text/javascript'>";
echo "top.location.href = '{$loginUrl}';";
echo "</script>";
}
为了实现这一目标,我需要做些什么改变?
答案 0 :(得分:1)
您有两种方法可以实现此行为。
/me/feed
端点。 (正如您在代码示例的try-catch
块中所做的那样)。 关于仅在用户首次访问时发布,您应该在数据库中存储一个布尔值。在数据库中为新用户创建新记录时,应包含一个名为first_visit
的字段,并使用“true”值填充该字段。
然后,当您检测到返回的用户(这意味着他已经在您的数据库中)时,您可以检查first_visit
字段是否设置为“false”。然后通过PHP SDK发布的帖子可以是条件表达式的结果,用于测试first_visit
值:
...
...
if ($first_visit == 'true'){
$result = $facebook->api('/me/feed', 'post', $vars);
}
另一个解决方案(不需要数据库)可能与此类似:
如果您使用$facebook->getLoginUrl()
方法为您的未授权用户巧妙地生成登录网址,则可以向GET
参数添加临时redirect_uri
参数。类似的东西:
$redirect_uri = 'https://apps.facebook.com/waffle-ville?new_user=true';
然后,您发布到用户墙的条件表达式将如下所示:
...
...
if ($_GET['new_user'] == 'true'){
$result = $facebook->api('/me/feed', 'post', $vars);
}
发布邮件后,请不要忘记将用户重定向回原始网址:
var app_url = "https://apps.facebook.com/waffle-ville";
echo "<script type='text/javascript'>";
echo "top.location.href = app_url;";
echo "</script>";
PHP也可以重定向:
$app_url = "https://apps.facebook.com/waffle-ville";
header("Location: {$app_url}");
IMO - 自动发布到用户墙壁有点烦人。应用程序设置中有一个名为Social Discovery
的参数。当此设置为“启用”时,只要用户安装您的应用程序,就会自动创建故事。我建议将发布作为可选的用户启动操作留给用户墙。
答案 1 :(得分:0)
我已经弄清楚了。我创建了一个存储信息的数据库,它检查用户ID是否已存在。如果没有,则将它们放在数据库中并在其墙上发布帖子。如果他们在数据库中,那么没有任何反应。
<?php
require 'facebook.php';
require 'dbconnect.php';
// Create our application instance
// (replace this with your appId and secret).
$app_id = "APP_ID";
$secret = "APP_SECRET";
$app_url = "APP_URL";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true,
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
$name = $user_profile['name'];
$birthday = $user_profile['birthday'];
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=> $app_url));
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
//DO NOT EDIT BELOW
$db=mysql_connect($hostname, $dbuser, $pass);
mysql_select_db($database, $db);
//check if user has already signed up before
$insert = true;
$result = mysql_query("SELECT * FROM table_name") or die(mysql_error());
while($row = mysql_fetch_array($result)){
//if user id exists, do not insert
if(( $row['UID'] == $user)){
$insert = false;
}
}
// if new user, insert user details in your mysql table
if($insert){
mysql_query("INSERT INTO table_name (UID, username, userbirthday) VALUES ('$user','$name','$birthday') ") or die(mysql_error());
$access_token = $facebook->getAccessToken();
$vars = array(
'message' => "message goes here",
'picture' => "image",
'link' => "Link here",
'name' => "Name here",
'caption' => "Caption here",
);
$result = $facebook->api('/me/feed', 'post', $vars);
}
?>