別のサーバーにswfmillをインストールしてswfとxmlの相互変換処理のみ実行させ、HTTP通信で送受信する方法について説明します。
単純にHTTP通信とswfmill変換処理を組み合わせただけです。
なお、今回のサンプルではリクエスト側とレスポンス側を同じサーバーに配置していますが、別のサーバーに配置しても問題なく動作します。
関連記事
HTTP通信の方法についてはこちら。
→参照:「swfmillの変換を別のサーバーで行うには(通信編)」
swfmillでのFlash動的変換についてはこちら。
→参照:「swfmillでケータイFlashを動的生成してみよう(文字列置換編)」
swfmillのインストール方法についてはこちら。
→参照:「swfmillでケータイFlashを動的生成してみよう(インストール編)」
動的変換する元SWFファイルの作成
内容を「{$text1}」としたダイナミックテキストをステージ中央に配置しました。
確認のためFlashLite1.1とFlashLite2.0の両方のバージョンを用意しておきました。
リクエスト側PHPの処理
1.SWFを読み込みます。
2.変換処理にSWFを送信してXMLを読み込みます。
3.XMLデータの内容を置換します。
「{$text1}」を任意の文字列に置換するようにしました。
4.変換処理にXMLを送信してSWFを読み込みます。
5.SWFを出力します。
以下のような記述になります。
sample3859.php
<?php /* * HTTP通信でのSWF変換処理のサンプル(リクエスト処理) */ // ライブラリ読み込み require_once 'Zend/Http/Client.php'; // 初期設定 // 呼び出しURL $responseUrl = 'http://変換スクリプトを配置したURL/sample3859_response.php'; // 生成するFlashLiteのバージョン $pattern = (int)$_GET['pattern']; // FlashLite1.1版の場合 // 読み込むSWFを指定 $originalSwfpath = 'sample3859_lite_1_1.swf'; // 変換エンコードはSJIS $encode = 'sjis'; // FlashLite2.0版の場合 if ($pattern === 1) { // 読み込むSWFを指定 $originalSwfpath = 'sample3859_lite_2_0.swf'; // 変換エンコードはUTF-8 $encode = 'utf8'; } // HTTP通信設定 $httpSendConfig = array( 'maxredirects' => 0, 'useragent' => 'PHP/' . phpversion(), 'timeout' => 120, ); // 元SWFのデータを取得 $swfData = file_get_contents($originalSwfpath); // XMLへの変換リクエスト $client = new Zend_Http_Client(); $client->setUri("{$responseUrl}?mode=swftoxml&encode={$encode}"); $client->setConfig($httpSendConfig); $client->setRawData($swfData, 'application/x-shockwave-flash'); $response = $client->request('POST'); $statusCode = $response->getStatus(); if ($statusCode !== 200) { throw new Exception('Failed to convert swftoxml'); } $xmlData = $response->getBody(); // 置換処理 // 置換文字列を設定 $replaceStrings = array( 'text1' => '携帯サイトをつくろう。', ); // 各置換文字列を置換 foreach($replaceStrings as $key => $value){ // 文字列を連想配列の値に置換 $xmlData = str_replace('{$' . $key . '}', $value, $xmlData); } // SWFへの変換リクエスト $client = new Zend_Http_Client(); $client->setUri("{$responseUrl}?mode=xmltoswf&encode={$encode}"); $client->setConfig($httpSendConfig); $client->setRawData($xmlData, 'text/xml'); $response = $client->request('POST'); $statusCode = $response->getStatus(); if ($statusCode !== 200) { throw new Exception('Failed to convert xmltoswf'); } $swfData = $response->getBody(); // 出力処理 header('Content-type: application/x-shockwave-flash'); echo $swfData; ?>
レスポンス側PHPの処理
こちらは
・SWFからXMLへの変換
・XMLからSWFへの変換
の2通りの処理が必要になります。
また、FlashLiteのバージョンによって変換エンコードを変更する必要があります。
swfmillの処理を関係のないサーバーから呼び出されないようにIP制限をかけています。
swfillのエラーを出力する「swfmill_error_log」は書き込み可能な権限で別に配置しておきます。
以下のような記述になります。
sample3859_response.php
<?php /* * HTTP通信でのSWF変換処理のサンプル(レスポンス処理) */ // 呼び出し元のIPを制限 if ($_SERVER['REMOTE_ADDR'] !== '呼び出し元サーバーIP') { exit; } $swfmillBinPath = '/usr/local/bin/swfmill'; // 変換モード $mode = $_GET['mode']; $commandMode = 'swf2xml'; if ($mode === 'xmltoswf') { $commandMode = 'xml2swf'; } // エンコード $encode = $_GET['encode']; $commandEncode = ''; if ($encode === 'sjis') { $commandEncode = ' -e cp932'; } // Flash変換エラーログファイルのパス $swfmillLogPath = 'swfmill_error_log'; $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("file", $swfmillLogPath, "a") ); // データを受信 $input = file_get_contents("php://input"); // swfmillコマンドを実行してリソースとファイルポインタを取得 $process = proc_open("{$swfmillBinPath} {$commandEncode} {$commandMode} stdin stdout", $descriptorspec, $pipes); if (is_resource($process)) { // 標準入力にSWFファイルのバイナリデータを書き込み fwrite($pipes[0], $input); fclose($pipes[0]); // 標準出力からXMLデータを読み込み $output = stream_get_contents($pipes[1]); fclose($pipes[1]); // パイプを全て閉じてからプロセスを閉じる proc_close($process); } // 出力 echo $output; ?>
実行例
実行例はこちらからご確認下さい。
→URLをメールで送る
http://ookura.tanikaze.com/Sample/post3859/