Passing FlashVars to Flex using php (w/o SWFObject)

Recommendation – use SWFObject to pass flashVars into a flex swf file application since it simplifies the whole process. The passing of data is centralized and taken care of for you in an OOP programmatic fashion.

My intent was to pass server data into a flex based application so that it could be configured dynamically on the fly.  I did not want to hardcode these  values into the application in case I moved the web application to anther URL.  I wanted to pass the server path and the php context root values as flashVars.

The first step was to generate a php file that would pass this information into the flex application. I started by grabbing the html code generated by the flex compiler. I then manually created a php file that would pass flashVars to a swf file by using this standard layout format(copied from the flex based generated html file).


<?php

//load root context constant 1-29-09 m2p
require_once 'AccessControl/root.inc.php';

//pass server path to flex swf
//$serverPath = $_SERVER&#91;'HTTP_HOST'&#93;.$_SERVER&#91;'PHP_SELF'&#93;;
$serverPath = $_SERVER&#91;'HTTP_HOST'&#93;;

//m2p 1-29-09 context root, set as constant in php define file
$contextRoot= CONTEXT_ROOT;

//to work in mozilla properly, must remove doctype statement
// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
//   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>NIM Course Administration</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta http-equiv="expires" content="0" />
<meta http-equiv="Pragma" content="no-cache">

<script src="AC_OETags.js" language="javascript"></script>
<style>
body { margin: 0px; overflow:hidden }
</style>
<script language="JavaScript" type="text/javascript">
<!--
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;
// -----------------------------------------------------------------------------
// -->
</script>

</head>

<body scroll="no">
<script language="JavaScript" type="text/javascript" src="history.js"></script>
<script language="JavaScript" type="text/javascript">
<!--
// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
var hasProductInstall = DetectFlashVer(6, 0, 65);

// Version check based upon the values defined in globals
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

// Check to see if a player with Flash Product Install is available and the version does not meet the requirements for playback
if ( hasProductInstall && !hasRequestedVersion ) {
// MMdoctitle is the stored document.title value used by the installation process to close the window that started the process
// This is necessary in order to close browser windows that are still utilizing the older version of the player after installation has completed
// DO NOT MODIFY THE FOLLOWING FOUR LINES
// Location visited after installation is complete if installation is required
var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
var MMredirectURL = window.location;
document.title = document.title.slice(0, 47) + " - Flash Player Installation";
var MMdoctitle = document.title;

AC_FL_RunContent(
"src", "playerProductInstall",
"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "AdminNim",
"quality", "high",
"bgcolor", "#ffffff",
"name", "AdminNim",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else if (hasRequestedVersion) {
// if we've detected an acceptable version
// embed the Flash Content SWF when all tests are passed
AC_FL_RunContent(
"src", "NimMemberEditor",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "AdminNim",
"quality", "high",
"bgcolor", "#ffffff",
"name", "AdminNim",
"flashvars",'historyUrl=history.htm%3F&lconid=' + lc_id + '&serverURL=<?php print $serverPath ?>&contextRoot=<?php print $contextRoot;?>',
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else {  // flash is too old or we can't detect the plugin
var alternateContent = 'Alternate HTML content should be placed here. '
+ 'This content requires the Adobe Flash Player. '
+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
document.write(alternateContent);  // insert non-flash content
}
// -->
</script>
<noscript>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"   
       id="NimMemberEditor" width="100%" height="100%"
       codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
       <param name="movie" value="NimMemberEditor.swf" />
       <param name="quality" value="high" />
       <param name="flashvars" value="serverURL=<?php $serverPath ?>&contextRoot=<?php $contextRoot;?">
       <param name="bgcolor" value="#ffffff" />
       <param name="allowScriptAccess" value="sameDomain" />
       <embed src="NimMemberEditor.swf" quality="high" bgcolor="#ffffff"      
           width="100%" height="100%" name="AdminNim" align="middle"   
           play="true"  loop="false"     
           quality="high"    
           flashvars="serverURL=<?php $serverPath ?>&contextRoot=<?php $contextRoot;?>"
           allowScriptAccess="sameDomain"    
           type="application/x-shockwave-flash"   
           pluginspage="http://www.adobe.com/go/getflashplayer">
      </embed>
</object>
</noscript>

</body>
</html>

The root.inc.php file is defined below:

<?php
//define context root
@define( 'CONTEXT_ROOT', '/' );
//server-live
//@define( 'CONTEXT_ROOT', '/nimphp/' );

?>

I got burned because I was not paying attention and only set the flashVars in one location in the php file. Unfortunately it was in the noscript section and the data was never being passed to the flex swf file application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: