Post 当用户允许我的应用时,如何只在用户墙上发布一次?

Post 当用户允许我的应用时,如何只在用户墙上发布一次?,post,facebook-php-sdk,facebook-apps,Post,Facebook Php Sdk,Facebook Apps,目前,每次用户访问我的应用程序时,我的应用程序都会贴到墙上。我只想在他们第一次授权该应用程序时将其发布到墙上一次。然后每次他们访问它之后,它只更新新闻提要状态 这是我目前的代码: // Get User ID $user = $facebook->getUser(); if ($user) { try { // Proceed knowing you have a logged in user who's authenticated. $access_token =

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

这是我目前的代码:

// 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>";
}
//获取用户ID
$user=$facebook->getUser();
如果($user){
试一试{
//继续,知道您有一个经过身份验证的登录用户。
$access_token=$facebook->getAccessToken();
$vars=数组(
'消息'=>“消息在这里”,
'图片'=>“图像”,
“链接”=>“此处链接”,
“名称”=>“此处的名称”,
“标题”=>“此处的标题”
);
$result=$facebook->api('/me/feed',post',$vars);
}捕获(FacebookApiException$e){
错误日志($e);
$user=null;
}
}
//根据当前用户状态,需要登录或注销url。
如果($user){
$logoutUrl=$facebook->getLogoutUrl();
}否则{
$loginUrl=$facebook->getLoginUrl(数组('redirect_uri'=>$app_url));
回声“;
echo“top.location.href='{$loginUrl}';”;
回声“;
}

为了实现这一点,我需要改变什么?

实现这一行为有两种方法可供选择

  • 为您的用户利用登录页上的。这将弹出一个Facebook窗口,提示你的用户在他们的墙上分享一些东西。此方法还要求您实现
  • 利用并以编程方式将提要故事发布到
    /me/feed
    端点。(正如您在代码示例的
    try catch
    块中所做的那样) 关于仅在用户首次访问时发布,您应该在数据库中存储一个布尔值。在数据库中为新用户创建新记录时,应包括一个名为“首次访问”的字段,并用“真”值填充该字段。
    然后,当您检测到返回的用户(这意味着他已经在您的数据库中)时,您可以检查
    first\u visit
    字段是否设置为“false”。然后,您通过PHP SDK发布的帖子可以是一个条件表达式的结果,用于测试
    first\u visit
    值:

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

    另一个解决方案(不需要数据库)可能与此类似:

    当您使用
    $facebook->getLoginUrl()
    方法巧妙地为未经授权的用户生成登录URL时,您可以在
    重定向uri
    参数中添加临时
    GET
    参数。比如:

    $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>";
    


    -自动发布到用户墙上有点烦人。应用程序设置中有一个参数称为
    社交发现
    。当设置为“enabled”时,用户一安装应用程序就会自动创建一个故事。我建议将发布作为用户发起的可选操作保留在用户墙上

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

    你能解释一下“只更新新闻提要状态”是什么意思吗?更新提要将创建一个新帖子。状态更新与墙上的任何其他帖子都是一样的——只由该用户发布,通常不包含任何链接。我的意思示例。我有scrabble应用程序,但当我使用该应用程序时,它不会发布到我的墙上,它会显示在新闻订阅源和我的活动日志中,说我玩了scrabble并链接到该应用程序。我希望你不要介意我稍微更改了你代码示例的格式;我发现在格式化方面多做一点努力,以后阅读起来就容易多了。尤其是在其他人阅读你的代码的情况下。撇开新闻提要问题不谈,我仍然不知道如何让我的应用程序在用户每次使用时都不会发布到用户墙上。我只想要他们授权应用程序时的初始墙贴。我不知道如何编辑我的当前代码来做这件事??@泰勒——如果你发现我的文章有帮助,你应该为看到我的帖子帮助你而感到兴奋:
    
    <?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);
    }
    
    ?>