【问题标题】:Calling Facebook PHP SDK 3 to pass user to AS3 Project调用 Facebook PHP SDK 3 将用户传递给 AS3 项目
【发布时间】:2011-12-21 14:23:38
【问题描述】:

我在将用户信息从 PHP 脚本加载到 Flash 时遇到问题。

编辑:Facebook 调用 index.php 进行身份验证。 Flash 然后调用 database.php 连接到数据库并存储/调用玩家信息和进度。然后,Database.php 对数组进行编码并将其回显到对其进行解析的 Flash。

这一直工作到上周末。

index.php

<?php
$app_id = "my_id";
$app_secret = "my_secret";
$my_url = "http://apps.facebook.com/my_app";

session_start();
$code = $_REQUEST["code"];

if(empty($code)) 
{
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
 $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
   . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
   . $_SESSION['state'];

 echo("<script> top.location.href='" . $dialog_url . "'</script>");
 exit;
}

if($_REQUEST['state'] == $_SESSION['state']) {
 $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

 $response = file_get_contents($token_url);
 $params = null;
 parse_str($response, $params);

 $graph_url = "https://graph.facebook.com/me?access_token=" 
   . $params['access_token'];

 $user = json_decode(file_get_contents($graph_url));
 //echo("Hello " . $user->name);
 include_once 'game.php';
}
else 
{
    echo("The state does not match. Try opening the app from the homepage.");
    include_once 'game.php';
}
?>

数据库.php

<?php
require 'src/facebook.php';

$facebook = new Facebook(array(
 'appId'  => 'my_id',
 'secret' => 'my_secret',
));

// Get User ID
try 
    {
    $user = $facebook->getUser();

    if ($user) 
        {
            $user_profile = $facebook->api('/me');

            //$friends = $facebook->api('/me/friends');
            //print_r($friends);
            mysql_connect("localhost", "DB_USER", "DB_PASS") or die("Could not connect");
        mysql_select_db("stus_zombies") or die("Could not select database");

        $query = mysql_query("SELECT * FROM users WHERE facebook_id = " . $user_profile[id]);  
        $result = mysql_fetch_array($query);

        if(empty($result))
        { 
            $addInfo = mysql_query("INSERT INTO users (first_name, last_name, facebook_id) 
            VALUES ('{$user_profile['first_name']}','{$user_profile['last_name']}','{$user_profile['id']}')");  
                $addInfo = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());  

            $query = mysql_query("INSERT INTO players (facebook_id, coins, health, stamina, xp)
            VALUES ('{$user_profile['id']}','0','25','25','0')");
        }

        $checkProgress = mysql_query("SELECT * FROM players WHERE facebook_id = " . $user_profile[id]);
        $playerCheck = mysql_fetch_array($checkProgress);

        $playerInfo = array(
        first_name => $user_profile[first_name],
            last_name => $user_profile[last_name],
            facebook_id => $user_profile[id], 
            //facebook_access_token => $_SESSION[fb_165114483572553_access_token],
            coins => $playerCheck[coins],
        health => $playerCheck[health],
        stamina => $playerCheck[stamina],
        xp => $playerCheck[xp],); 
        echo json_encode($playerInfo);
        } 

    else
    {
        echo ("No User");
    }
}

catch (FacebookApiException $e) 
{
    error_log($e);
        $user = null;
    }
?>

我更改了一种不同的身份验证方法,并且在停止之前工作了几个小时。

我尝试的第二个 index.php:

 <?php

 $app_id = "165114483572553";

 $canvas_page = "https://apps.facebook.com/MY_APP";

 $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
        . $app_id . "&redirect_uri=" . urlencode($canvas_page);

 $signed_request = $_REQUEST["signed_request"];

 list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

 $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

 if (empty($data["user_id"])) {
        echo("<script> top.location.href='" . $auth_url . "'</script>");
 } else {
 include_once 'game.php';
 } 
 ?>

AS3 类数据库.as

package  
{
import flash.events.IOErrorEvent;
import com.adobe.webapis.URLLoaderBase;
import flash.events.Event;
import flash.net.*;
import com.adobe.serialization.json.JSON;
import org.flashdevelop.utils.FlashConnect;
import HUD.UI;

public class Database
{
    // Page Loader
    private static var loader:URLLoader;
    private static var dataloader:URLLoader;

    // User Info
    private static var playerData:Object;

    //TEST
    // Player Info
    private static var playerLoader:URLLoader = new URLLoader();
    private static var playerRequest:URLRequest = new URLRequest;
    playerRequest.url = "http://MYURL/database.php";

    public function Database() 
    {

    }

    public function getPlayer():void
    {
        playerLoader.load(playerRequest);
        playerLoader.addEventListener(Event.COMPLETE, decodeJSONPlayer);
        playerLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    }
    // Decode JSON (Player Coins, ID, Stamina, Health, XP)
    private function decodeJSONPlayer(e:Event):void
    {
        playerLoader = URLLoader(e.target);
        playerData = JSON.decode(playerLoader.data);
        loadPlayerInfo(playerData);
        // Display stats info
        UI.updateHealth();
        UI.updateMoney();
        UI.updateXP();
        UI.updateStamina();
    }

    // Load database info into player's data
    private function loadPlayerInfo(playerData:Object):void
    {
        Player.first_name = playerData.first_name;
        Player.last_name = playerData.last_name;
        Player.facebook_id = playerData.facebook_id;
        Player.facebook_access_token = playerData.facebook_access_token;
        Player.health = playerData.health;
        Player.stamina = playerData.stamina;
        Player.money = playerData.coins;
        Player.xp = playerData.xp;
        trace(playerData.first_name);
        // Display Facebook info
        UI.txtFacebookInfo.text = playerData.first_name +" " + playerData.last_name +" " + playerData.facebook_id;
    }

    // Save all Progress
    public static function setAll(_money:int, _health:int, _stamina:int, _xp:int):void
    {
        var vars:URLVariables = new URLVariables();
        vars.money = _money;
        vars.health = _health;
        vars.stamina = _stamina;
        vars.xp = _xp;

        var request:URLRequest = new URLRequest("http://MYURL/saveplayer.php");
        request.method = URLRequestMethod.POST;
        request.data = vars;

        if (loader != null)
        {
            loader.removeEventListener(Event.COMPLETE, pageLoaded);
        }

        loader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.TEXT;
        loader.load(request);
        loader.addEventListener(Event.COMPLETE, pageLoaded);
    }

    private static function pageLoaded(e:Event):void
    {
        trace("PAGE LOADED\n============================");
        FlashConnect.trace(loader.data);
        trace("============================");
    }

    private static function completeHandler(event:Event):void
    {
        var loader:URLLoader = URLLoader(event.target);
        FlashConnect.trace(loader.data.dayNames);
    }

    // Server info not received
    private function onIOError(e:Event):void
    {
        trace ("Error loading URL");
    }
}

}

将其更改回原始代码一直有效,直到第二天停止工作。

我设法通过将数据库脚本包含在索引脚本中,然后从 Flash 中调用它来临时修复它。

这只是停止工作。显然,这让我发疯了。即使现在在 index.php 中包含 database.php 也会 trace 正确地回显 JSON 信息,但不会加载到游戏中。

在 Flash 方面,我将它连接到一个按钮,这样我就可以调用它并查看返回,它似乎工作正常。当它不加载播放器的信息时,我永远不会返回错误,这使得这个试验和错误和研究成为故障排除。

任何帮助将不胜感激。当有人知道发生了什么时,我花了太多时间调整脚本和研究。

【问题讨论】:

  • 如果您将 JSON 数据放入 Database.as 响应中,那么您为什么认为这是服务器端问题?你得到了你需要的数据。您可能没有正确解析它。
  • 对不起,当我说追踪我的意思是它在浏览器中回显。如果我添加要编码的虚拟信息,它将在游戏中正确更新。

标签: php facebook actionscript-3 facebook-php-sdk oauth-2.0


【解决方案1】:

这是一个域问题,Facebook 有时使用 ssl 调用我的域,这与不安全域不同。通过强制它和我的应用调用安全域来解决。

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    相关资源
    最近更新 更多