-6

我有一个程序(一个应用程序),它要求用户输入然后生成图像并将图像链接发布到 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 '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

            // Give the user a logout link 
            echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
          } catch(FacebookApiException $e) {

            $login_url = $facebook->getLoginUrl( array(
                           'scope' => 'publish_stream'
                           )); 
            echo 'Please <a href="' . $login_url . '">login.</a>';
            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 <a href="' . $login_url . '">login.</a>';

        } 

        } 

        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"));
4

2 回答 2

0

我认为代码是这样的:

$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 '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

        //save post_id to MySQL
        $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"));
        //save post_id to MySQL

        // Give the user a logout link 
        echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
      } catch(FacebookApiException $e) {

        $login_url = $facebook->getLoginUrl( array(
                       'scope' => 'publish_stream'
                       )); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        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 <a href="' . $login_url . '">login.</a>';

    } 

    } 

    mysqli_close($con);
}

您可以创建一些功能来发布FB。我使用 curl 发布 FB,而不是使用 FB PHP SDK。代码

$arr['token'] = {your token};
$arr['content'] = {content};
$arr['url'] = {share url};

//save post_id to MySQL
$post_id = fb_post($arr);
$save_id = "INSERT INTO `mytable` (fb_id) VALUES ('$post_id')";
$query = mysql_query($save_id,$con) or die(mysql_error("Could not write information to the database"));
//save post_id to MySQL

function fb_post($post){
    $arr['url'] = "https://graph.facebook.com/me/feed/";
    $arr['content'] = array(
        'access_token'=>$post['token'],
        'message' => $post['content'],
        'link' => $post['url']
    );
    $re = fb_post_c($arr);
    $temp = json_decode($re);
    return $temp->id;
}
function fb_post_c($arr){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $arr['url']);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, 'Expect:');
    curl_setopt($ch, CURLOPT_POSTFIELDS ,http_build_query($arr['content']));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $re = curl_exec($ch);
    curl_close($ch);
    return $re;
}

使用 fb php sdk

session_start();
require_once 'facebook.php';
$config = array();
$config['appId'] = {your appid};
$config['secret'] = {your secret};

$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id){
    $_SESSION['token'] = $facebook->getAccessToken();
    $arr['content'] = {content};
    $arr['url'] = {share url};
    $post_id = fb_post_c($arr);
    if($post_id){
        db_in($post_id);
        echo 'your post id' . $post_id;
    }
}else{
    $params = array(
        'scope' => 'publish_stream,user_activities',
        'redirect_uri' => 'http://XXXXXXXXXXX/'
    );
    $facebook->setExtendedAccessToken();
    $loginUrl = $facebook->getLoginUrl($params);
    echo "<a href ='".$loginUrl."'>login</a><br />";
}
function db_in($post_id){
    global $con;//your database ini
    $save_id = "INSERT INTO `mytable` (fb_id) VALUES ('$post_id')";
    $query = mysql_query($save_id,$con) or die(mysql_error("Could not write information to the database"));
}
function fb_post_c($arr){
    global $facebook;
    try {
        $ret_obj = $facebook->api('/me/feed', 'POST',
                    array(
                        'link' => $arr['url'],
                        'message' => $arr['content']
                    ));
        return $ret_obj['id'];
    } catch(FacebookApiException $e) {
        return false;
    }
}
于 2013-11-05T09:37:02.143 回答
-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 '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

            // Give the user a logout link 
            echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
          } 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 <a href="' . $login_url . '">login.</a>';
            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 <a href="' . $login_url . '">login.</a>';

        } 
        $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"));
于 2013-11-05T08:41:47.683 回答