如果publish_stream成功,只想添加到mysql数据库

时间:2014-07-30 17:30:19

标签: mysql facebook-php-sdk

请你帮我。我创建了一个Facebook应用程序发布到墙上并向mysql数据库添加详细信息但是目前用户必须对应用程序进行身份验证,并且这样做会将空白数据发送到数据库。如何在发布权限时更新数据库。

<?php
include_once("src/facebook.php"); //include facebook SDK

$config = array(
     'appId' => 'xxxx',
    'secret' => 'xxxx',
    'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
  );


  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();



  $formemail = $_POST['formemail'];
  $formname = $_POST['formname'];
  $formphone = $_POST['formphone'];

  echo $email;
  echo $formname;
  echo $formemail;
  echo $formphone;

?>
<html>
  <head>

    <link rel="stylesheet" type="text/css" media="all" href="css/style.css">


       <script type="text/javascript" src="jhttp://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


  </head>
  <body>




  <script type="text/javascript">
    opener.location.reload();
    window.close();
</script>



  <?php
    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' => 'https://apps.facebook.com/ahora-opening-comp',
                                      'message' => 'I have just entered the competition to WIN a table for 8 at Ahora on the 28th September at O2 Accademy, Sheffield '
                                 ));


        // Give the user a logout link 
            ?>
        <!-- html POsted to wall -->

<div class="wrapper">  
<div class="container container-thanks">
<div class="thankyou">



<?php

        echo '<br /><a href="' . $facebook->getLogoutUrl() . '"><img src="images/logoutfb.png"></a>';

        $user_profile = $facebook->api('/me');
        $email = $user_profile['email'];
        echo $email;


          //Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "xxxxx";
$username = "xxxx";
$dbname = "xxxxx";

//These variable values need to be changed by you before deploying
$password = "xxxxx!";



$con=mysqli_connect("$hostname","$username","$password","$dbname");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO users (id ,  formname , formemail , fbemail, formphone , date)
VALUES (NULL, '$formname', '$formemail' , '$email', '$formphone' , NOW())";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}


      } 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.

            ?>
        <!-- html POsted to wall -->

<div class="wrapper">  
<div class="container container-share">
<div class="thankyou">

<?php

      $login_url = $facebook->getLoginUrl( array( 
                        'display' => 'popup',
                        'scope' => 'publish_actions, email',
                        'redirect_uri' => 'http://ibizasocial.com/ahora/FBapps/fb-opening/submit.php'
                         ));

              echo '<a href="#" onclick="window.open(\''.$facebook->getLoginUrl(array('display' => 'popup', 'scope' => 'publish_actions',)).'\', \'Facebook\', \'height=400,width=600\');"><img src="images/loginfb.png"></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_actions permission
      // We'll use the current URL as the redirect_uri, so we don't
      // need to specify it here.

        ?>
        <!-- html POsted to wall -->

<div class="wrapper">  
<div class="container container-share">
<div class="thankyou">

<?php


      $login_url = $facebook->getLoginUrl( array( 
                        'display' => 'popup',
                        'scope' => 'publish_actions, email',
                        'redirect_uri' => 'http://ibizasocial.com/ahora/FBapps/fb-opening/submit.php'
                         ));

              echo '<a href="#" onclick="window.open(\''.$facebook->getLoginUrl(array('display' => 'popup', 'scope' => 'publish_actions',)).'\', \'Facebook\', \'height=400,width=600\');"><img src="images/loginfb.png"></a>';


    } 

  ?>   

  </div></div>

  <div>
  <a href="https://www.iubenda.com/privacy-policy/429825" target="_blank">Click here to view Privarcy Policy </a></div>
  </div>   

  </body> 
</html>  

1 个答案:

答案 0 :(得分:0)

https://developers.facebook.com/docs/graph-api/reference/v2.0/user/feed

根据Facebook文档,返回值是帖子的ID,如果成功,则为字符串值。