2008.04.23
category
comments

PHPに複数データを渡してみる

AS3ではURLRequest.dataで送信できるデータは1つなので、複数の変数をPHPに渡したい場合はURLVariablesを使う。URLVariablesをインスタンス化した後に、通常のオブジェクトと同様、任意のプロパティに値を格納していく。ここでは「title」、「url」、「text」の3つの要素を各プロパティに入れている。URLVariablesに格納した後は、URLRequestにて呼び出したいPHPのパスと通信方法を指定(今回はPOST通信)し、URLRequest.dataにURLVariablesを突っ込む。最後にnavigateToURL()でPHPを表示させ、Flashからデータを受け取っているか確認する。

Main.as

ACTIONSCRIPT:
package
{
    import flash.display.Stage;
    import flash.display.Sprite;
    import flash.net.URLVariables;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.navigateToURL;
   
   
    public class Main extends Sprite
    {
        //------------------------------
        //   コンストラクタ
        //------------------------------
        public function Main()
        {
            sendData();
        }
       
        //------------------------------
        //   データ送信
        //------------------------------
        public function sendData():void
        {
            var variables:URLVariables = new URLVariables();
            variables.title = "5ive.blog";
            variables.url = "http://www.5ive.info/blog/";
            variables.text = "PHPに複数のデータを渡す方法";
           
            var urlRequest:URLRequest = new URLRequest();
            urlRequest.url = "variables.php";
            urlRequest.method = URLRequestMethod.POST;
            urlRequest.data = variables;
           
            //phpに送信
            navigateToURL(urlRequest , "_self");
        }
    }
}

variables.php

PHP:
<?php
    $title = $_POST['title'];
    $url = $_POST['url'];
    $text = $_POST['text'];
   
    echo "title = ".$title."<br />";
    echo "url = ".$url."<br />";
    echo "text = ".$text."<br />";
?>

Post a comment




Comment

Trackbacks

この記事のコメント・トラックバックRSS