HTMLからFlexに値を渡す方法。
#下に「さらにPHP(Web)へのリクエストをそのままFlexに渡す方法」を追記してます。
サイトをPHPで作成していると、同種類のページが10も20もできて、スライドショーもページごとに必要になってくるケースがあります。
グルメGyaoがそんな感じだと思います。
毎回、Flashをページごとにパブリッシュするのは時間がかかるので、できれば動的なFlashが望ましいと思います。
こんな感じ(htmlからFlexへ値を渡すことができます。
http://moeten.info/flex/20080708_paramTest/bin-release/main.html
値はURLのクエリのような形式で渡します。
"FlashVars" , "sid=13&m=moe",
HTMLのソース
Flexプロジェクトのhtml-templateの中にあるindex.tmplate.htmlを編集します。
htmlファイルにいくつかパラメーターを設定できそうなところがあるので、そこにパラメーターを追加してあげます。
AC_FL_RunContent(
"src", "${swf}",
"width", "${width}",
"height", "${height}",
"align", "middle",
"id", "${application}",
"quality", "high",
"bgcolor", "${bgcolor}",
"name", "${application}",
→これを追加 "FlashVars" , "sid=13&m=moe",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
noscript部分
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="${application}" width="${width}" height="${height}" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> <param name="movie" value="${swf}.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="${bgcolor}" /> →これを追加 <param name="FlashVars" value="sid=13&m=moe" /> <param name="allowScriptAccess" value="sameDomain" /> <embed src="${swf}.swf" quality="high" bgcolor="${bgcolor}" width="${width}" height="${height}" name="${application}" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"> </embed> </object>
Flexのソース
ActionScriptの場合はLoaderInfo.parameters
Flexの場合はthis.parametersでFlashVarsの値が取得できます。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Script> <![CDATA[ //簡単デバッグクラス import com.flexer.Debug; //初期化関数 private function init():void{ myLog.text = Debug.dump(this.parameters ); } ]]> </mx:Script> <mx:TextArea id="myLog" x="10" y="10" width="388" height="432"/> </mx:Application>
parametersにObject形式でFlashVarsの値が入ります。
デバッグにはflexerのデバッグクラスが便利なので今回はそちらを使っています。
ちなみに、デバッグクラスを使わなくても普通に this.parameters["sid"] や this.parameters.sid でオブジェクトの中身を表示することもできます。
さらにPHP(Web)へのリクエストをそのままFlexに渡す方法。
PHPへリクエストされた情報は $_SERVER の "QUERY_STRING" に格納されるのでそれをFlashVarsに割り当てます。
サンプルはこちら
http://moeten.info/flex/20080708_paramTest/bin-release/urltest.php?m=haruhi&sid=10
urltest.phpの後ろの文字は自由に変更できます。
PHPなソースはこちら
単純にFlashVarsの部分に $_SERVER["QUERY_STRING"] を入れています。
<?php $query = $_SERVER["QUERY_STRING"]; echo <<<EOD <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="history/history.css" /> <title></title> <script src="AC_OETags.js" language="javascript"></script> <script src="history/history.js" language="javascript"></script> <style> body { margin: 0px; overflow:hidden } </style> <script language="JavaScript" type="text/javascript"> <!-- var requiredMajorVersion = 9; var requiredMinorVersion = 0; var requiredRevision = 28; --> </script> </head> <body scroll="no"> <script language="JavaScript" type="text/javascript"> <!-- var hasProductInstall = DetectFlashVer(6, 0, 65); var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision); if ( hasProductInstall && !hasRequestedVersion ) { 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", "main", "quality", "high", "bgcolor", "#869ca7", "name", "main", "FlashVars" , "{$query}", "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", "main", "width", "100%", "height", "100%", "align", "middle", "id", "main", "quality", "high", "bgcolor", "#869ca7", "name", "main", "FlashVars" , "{$query}", "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="main" width="100%" height="100%" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> <param name="movie" value="main.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#869ca7" /> <param name="FlashVars" value="{$query}" /> <param name="allowScriptAccess" value="sameDomain" /> <embed src="main.swf" quality="high" bgcolor="#869ca7" width="100%" height="100%" name="main" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"> </embed> </object> </noscript> </body> </html> EOD; ?>