当用户允许我的应用程序时,如何仅向用户墙发布 1 次?

发布于 2025-01-04 16:55:16 字数 1035 浏览 1 评论 0原文

目前,我的应用程序每次访问应用程序时都会发布到用户墙。我只希望在他们第一次授权该应用程序时将其发布到墙上一次。然后每次他们访问它之后,它只会更新新闻提要状态。

这是我当前的代码:

// 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>";
}

我需要更改什么才能实现这一点?

Currently my app posts to the users wall every time they access the app. I only want it to post to the wall one time when they first authorize the app. Then every time they access it afterward it only updates the news feed status.

here is my current code:

// 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>";
}

What do I need to change in order to make that happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无需解释 2025-01-11 16:55:16

您有两种方法可以选择来实现此行为。

  1. 利用用户登陆页面上的Feed 对话框。这将弹出一个 Facebook 窗口,提示您的用户在他们的墙上分享一些内容。此方法要求您还实现 JavaScript SDK
  2. 利用 PHP SDK 并以编程方式将 feed 故事发布到 /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() 方法为未经授权的用户生成登录 URL 时,您可以向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);
}

发布帖子后,不要忘记将用户重定向回原始 URL:

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 - 自动发布到用户墙有点烦人。您的应用程序设置中有一个名为社交发现的参数。当此设置为“启用”时,一旦用户安装您的应用程序,就会自动创建故事。我建议将发布到用户墙作为可选的用户启动操作。

You have 2 choices of methods to achieve this behavior.

  1. Utilize the Feed Dialog on the landing page for your users. This will popup a Facebook window prompting your users to share something on their wall. This method requires that you implement the JavaScript SDK as well.
  2. Utilize the PHP SDK and programatically posting a feed story to the /me/feed endpoint. (As you have done in the try-catch block of your code sample).

With regard to only posting on the users first visit you should store in your database a boolean value. When you create a new record for the new user in your database you should include a field called something like first_visit and populate it with a "true" value.
Then when you detect a returning user (that means he is already in your database) you can check to see that the first_visit field is set to "false". Then your post via the PHP SDK can be the result of a conditional expression to test the first_visit value :

...
...
if ($first_visit == 'true'){
  $result = $facebook->api('/me/feed', 'post', $vars);
}

An additional solution (not requiring a database) could be something similar to this :

When you so cunningly generate the login URL with the $facebook->getLoginUrl() method for your un-authorized users, you can add a temporary GET parameter to the redirect_uri parameter. Something like :

$redirect_uri = 'https://apps.facebook.com/waffle-ville?new_user=true';

Then your conditional expression for posting to the users wall would look something like this :

...
...
if ($_GET['new_user'] == 'true'){
  $result = $facebook->api('/me/feed', 'post', $vars);
}

Don't forget to redirect the user back to the original URL after you have made the post :

var app_url = "https://apps.facebook.com/waffle-ville";
echo "<script type='text/javascript'>";
echo     "top.location.href = app_url;";
echo "</script>";

The redirect is also possible with PHP :

$app_url = "https://apps.facebook.com/waffle-ville";
header("Location: {$app_url}");

IMO - Posting to a users wall automagically is a little bit annoying. There is a parameter in your application settings that is called Social Discovery. When this is set to "enabled" a story is automagically created as soon as a user installs your application. I recommend leaving posting to a users wall as an optional user initiated action.

人海汹涌 2025-01-11 16:55:16

我已经弄清楚了。我创建了一个数据库来存储信息,并检查用户 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);
}

?>

I've figured it out. I created a database to store info and it checks to see if the User ID already exists or not. If it doesn't, then they are placed in the database and a post is made to their wall. If they are in the database, then nothing happens.

<?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);
}

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文