简体   繁体   中英

Passing Flash variables to PHP

I have a simple standalone application written in Visual Basic that I'm porting to a browser based application using PHP/javascript.

The original VB application has some simple embedded flash games with token and point counters. The token and point values are being passed as variables between the application and the game.

I'm trying to achieve the same effect in my PHP port without modifying the actionscript code( using the variables in actionscript that already exist).

Below is Visual Basic code that's loading a value from a database and posting that value to flash using FlashVars:

Private Sub loadPlayer()

    Try

        If CtblPoints.CheckPointsByID(mCard) Then

            objPoints = CtblPoints.GettblPointsByID(mCard)
            objPlayerAc = CtblPlayerAccount.GettblPlayerAccountByPlayerID(objPoints.AccountId)
            objPlayer = CtblPlayer.GettblPlayerByID(objPlayerAc.PlayerID)
            objPlayerBal = CtblPlayerBalance.GettblPlayerBalanceByID(objPlayerAc.PlayerID)


            objPlayerAcDetail = CtblPlayerAccountDetail.GettblPlayerAccountDetailByAmount(objPoints.AccountId)
            strTotalPoints = Convert.ToString(objPlayerAc.Points)
            strTotalWin = Convert.ToString(objPlayerBal.TokenAmount)

            'Dim intTokenAmount As Decimal = Convert.ToDecimal(objPlayerBal.TokenAmount)
            'strTotalWin = Convert.ToString(Convert.ToInt64(intTokenAmount * 100))

            flashPlayer.Size = panelGame.Size
            flashPlayer.FlashVars = "totalEntries=" & strTotalPoints & "&credit=" & strTotalWin
            flashPlayer.LoadMovie(0, strGameFile)
            flashPlayer.Play()

        Else
            Me.Close()
            Dim frmInvCrd As New frmInvalidCard
            frmInvCrd.ShowDialog()
        End If


    Catch ex As Exception

    End Try

I'm trying to recreate this in PHP, but I'm at a loss as to how to begin implementing it.

The variables in flash are declared publicly, and global imports used:

import com.atticmedia.console.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.media.*;
import flash.net.*;
import flash.system.*;
import flash.utils.*;

First declaration of variable 'totalEntries' is:

public var totalEntries:int = 0;

and this is a snip of totalEntries being used in the actionscript

    public function notifyServerOfUnwonCredits(param1)
    {
        var remainder:* = param1;
        if (this.useServer)
        {
            this.targetWinAmount = 0;
            this.cancelUpdateOverTime = F9.setEnterFrame(this.updateOverTime);
            fscommand("OverTime", "" + remainder);
            this.flashVarsUpdatedAction = function ()
        {
            originalTotalWin = totalWin;
            return;
        }// end function
        ;
        }
        else
        {
            this.setTotalEntries(100000);
            this.setTotalWin(0);
        }
        return;
    }// end function

Eventually I'll be passing these values back to a mySQL database.

Any insight into this would be extremely helpful, Thanks!

PHP Doesn't work like that; it's not going to be sitting around, in a persistent state that you can always contact and talk to. You'll need a mechanism to send and receive data to the stateless backend. Now, luckily, flash works perfectly well with cookies, so using PHP sessions doesn't produce an issue. So, the question is whether you want to pass the data to PHP via Flash, or pass the variables to Javascript via flash and then send them to PHP.

Direct-via-flash route (bear with me, this is AS 2.0, but should work):

Actionscript:

function getData (path:String, req:Object, fn:Function) {
   var q:Object = new LoadVars();
   for (var prop in req) {
       q[prop] = req[prop];
   }
   q.action = path;
   q.onLoad = fn;
   q.sendAndLoad("/hooks/integrate.php",q,"POST");
}

in this example, I pass to a single file and specify a POST parameter, "action", that tells the handling file what it is I want to do.

<?php
if (!isset($_POST['action'])) { die; }
switch($_POST['action']) {
    case 'something':
// snip...
}

And then calling the function is a snap:

getData('saveVar',{varname: 'foo', varvalue: foo},mySuccessFunction);

Alternatively, you could set something up using XHR/AJAX and pass the variables to Javascript by using the flash.external.ExternalInterface.call() method, but setting up such a solution is beyond the scope of this answer.

Long story short, there is no quick and easy way to do it like you did in VB due to PHP's nature as a stateless request-based web service; you will have to employ another mechanism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM