是否有可能/如何将 Facebook 帖子 ID 保存到 mysql 数据库?

分享于2023年05月08日 facebook facebook-graph-api mysql php 问答
【问题标题】:Is it possible/How to save Facebook Post ID to mysql database?是否有可能/如何将 Facebook 帖子 ID 保存到 mysql 数据库?
【发布时间】:2023-05-07 20:31:01
【问题描述】:

我有一个程序(应用程序),它要求用户输入然后生成图像并将图像链接发布到 FB 时间线。现在我要做的是获取 POSTID 并将其保存到我的数据库中。所以我可以有一个记录,如果我想连接到 FB POST,我可以检索它。我怎样才能做到这一点?以下是我用于发布到 Facebook 时间线的代码。希望你能帮我解决这个问题。我第一次做这样的事情。非常感谢!

    $http_dir = 'http://site.com/Amats/image_entry/';
    $post_link = $http_dir . $img_newname;

    if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($story)) {

        $save_sql = "INSERT INTO `tbl_contest` (filename, name, email, office_id, title, story, time) VALUES ('$img_newname','$name','$email','$office_id','$title','$story','$sql_date')";

        $query = mysql_query($save_sql,$con) or die(mysql_error("Could not write information to the database")); 

        if (mysql_num_rows($con) !== 0) { 
        header('Location:' . $uploadSuccess.'#modal-text'); 
        if($user_id) {

          // We have a user ID, so probably a logged in user.
          try {
            $ret_obj = $facebook->api('/me/feed', 'POST',
                                        array(
                                          'link' => $post_link,
                                          'message' => 'Posting with the PHP SDK!'
                                     ));
            echo '
Post ID: ' . $ret_obj['id'] . '
'; // Give the user a logout link echo '
logout'; } catch(FacebookApiException $e) { $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' )); echo 'Please login.'; error_log($e->getType()); error_log($e->getMessage()); } } else { // No user, so print a link for the user to login $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) ); echo 'Please login.'; } } mysqli_close($con); }

我试着做这样的东西。但它不起作用;

        $post_id = $ret_obj['id'];
        $save_id = "INSERT INTO `tbl_mytable` (post_id) VALUES ('$post_id')";
        $query = mysql_query($save_id,$con) or die(mysql_error("Could not write information to the database"));

  • 您到底遇到了什么错误?为什么使用 mysql_* 函数?使用 php.net/manual/en/book.mysqli.php php.net/manual/en/ref.pdo-mysql.php
  • 我没有收到任何错误。这是我认为的另一个问题。我会看看你的链接。但基本上,我是否以正确的方式获取帖子 ID?谢谢
  • 我的其他查询链接将用户输入插入数据库很好。所以我在想如果我在获取 Facebook 帖子 ID 的方式上做错了什么
  • @Pavan Kumar 我没有收到任何错误。正在保存应保存到数据库的其他详细信息。只有 POSTID 没有被保存。
  • 尝试打印 $ret_obj 并查看是否获得了 'id' 的值

【解决方案1】:

我正在尝试制作这样的东西

        if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($story)) {

        $save_sql = "INSERT INTO `tbl_contest` (filename, name, email, office_id, title, story, time) VALUES ('$img_newname','$name','$email','$office_id','$title','$story','$sql_date')";

        $query = mysql_query($save_sql,$con) or die(mysql_error("Could not write information to the database")); 

        if (mysql_num_rows($con) !== 0) { 
        header('Location:' . $uploadSuccess.'#modal-text'); 
        if($user_id) {

          // We have a user ID, so probably a logged in user.
          // If not, we'll get an exception, which we handle below.
          try {
            $ret_obj = $facebook->api('/me/feed', 'POST',
                                        array(
                                          'link' => $post_link,
                                          'message' => 'Posting with the PHP SDK!'
                                     ));
            echo '
Post ID: ' . $ret_obj['id'] . '
'; // Give the user a logout link echo '
logout'; } catch(FacebookApiException $e) { // If the user is logged out, you can have a // user ID even though the access token is invalid. // In this case, we'll get an exception, so we'll // just ask the user to login again here. $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' )); echo 'Please login.'; error_log($e->getType()); error_log($e->getMessage()); } } else { // No user, so print a link for the user to login // To post to a user's wall, we need publish_stream permission // We'll use the current URL as the redirect_uri, so we don't // need to specify it here. $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) ); echo 'Please login.'; } $get_id = $ret_obj['id']; $getid_sql = "INSERT INTO `tbl_contest` (post_id) VALUES ('$get_id')"; $getquery = mysql_query($getid_sql,$con) or die(mysql_error("Could not write information to the database")); } mysqli_close($con); }

我只是在前面的代码中添加了这 3 行代码:

        $get_id = $ret_obj['id'];
        $getid_sql = "INSERT INTO `tbl_contest` (post_id) VALUES ('$get_id')";
        $getquery = mysql_query($getid_sql,$con) or die(mysql_error("Could not write information to the database"));

【讨论】: