Facebook获取用户名并将其写入数据库

时间:2013-05-09 10:03:25

标签: facebook actionscript-3 flash actionscript username

我正在为Facebook创建Flash游戏。我需要获取Facebook用户名并将其写入mysql数据库,但这里我有问题。

这是我展示游戏并从Facebook获取用户名的代码。 fb.php

 <?php
     require_once('images/Facebook.php');

  $facebook = new Facebook(array(
    'appId'  => '156187751211405',
    'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
  ));

  # Get User ID
  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();


    if($user_id) {
$redirectUri = 'http://www.facebook.com/JuokoEra/app_156187751211405?ref=ts';

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {

        $user_profile = $facebook->api('/me','GET');
        echo "Name: " . $user_profile['name'];
  $name = "".$user_profile['name']."";
      } 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(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {

      // No user, print a link for the user to login
    $loginUrl = $facebook->getLoginUrl( array(
        'scope' => 'publish_stream,photo_upload',
        'redirect_uri' => $redirectUri
    ));
    }
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-8" />
<title>Eurokos</title>
</head>
<body bgcolor="#ffffff">
<!--url's used in the movie-->
<!--text used in the movie-->
<!-- saved from url=(0013)about:internet -->
<center>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="643" id="Eurokos" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="Memory22.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="Memory22.swf" quality="high" bgcolor="#ffffff" width="550" height="643" name="Eurokos" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</center>
</body>
</html>

这是我的gameinfo.php,我从flash游戏获取时间变量,尝试获取Facebook用户名(不成功)并将其写入数据库。

<?php

    require_once('images/Facebook.php');

  $facebook = new Facebook(array(
    'appId'  => '156187751211405',
    'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
  ));

  # Get User ID
  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();

?>
<html>
  <head></head>
  <body>

  <?php
    if($user_id) {
$redirectUri = 'http://www.facebook.com/JuokoEra/app_156187751211405?ref=ts';
    $loginUrl = $facebook->getLoginUrl( array(
        'scope' => 'publish_stream,photo_upload',
        'redirect_uri' => $redirectUri
    ));
      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {

        $user_profile = $facebook->api('/me','GET');
        echo "Name: " . $user_profile['name'];
  $name = "".$user_profile['name']."";
      } 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(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {

      // No user, print a link for the user to login
      $login_url = $facebook->getLoginUrl();
      echo 'Please <a href="' . $login_url . '">login.</a>';

    }

  ?>

  </body>
</html>
    <?php


        $time = $_POST['time'];
    $username = $_POST['userName'];


    $con=mysqli_connect("localhost","padekime_db","password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
    mysqli_query($con,"INSERT INTO eurokos (time, userName)
    VALUES ('333', '$name')"); 
    $query = "INSERT INTO eurokos VALUES" . "('$time', '$username')";
    echo "success=true";    

?>

我需要得到用户的任何许可如果我想拿他的Facebook用户名?怎么做?你可以帮帮我吗?谢谢。

0 个答案:

没有答案