falsh actionscript 与服务器端语言交互 与php 交互示例

本文介绍了一个使用ActionScript从Flash调用PHP脚本的例子。通过POST方式发送请求,并展示如何解析返回的JSON数据。
package
{
	import com.adobe.serialization.json.JSON;	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;	
	public class AsInvokePHP extends MovieClip
	{
		public function AsInvokePHP()
		{
			var url = "http://localhost:8190/api.php?";
			var params = "cmd=actionscript调用PHP了";
			call(url,params,function(e:Event)
			{
				var otmp:Object =JSON.decode(e.currentTarget.data);
				trace(otmp.cmd);
			});
		}		
		/**
		 * @postUrl 请求的地址;</br>
		 * ex:http://localhost:8190/api.php?
		 * @params  请求的参数; </br>
		 * ex:cmd=actionscript调用PHP了
		 * */
		function call(postUrl:String,params:String,fn:Function):void
		{						
			var sendRequest:URLLoader = new URLLoader();						
			var urlReq:URLRequest = new URLRequest(postUrl);
			urlReq.data = new URLVariables(params);
			urlReq.method = URLRequestMethod.POST;
			sendRequest.addEventListener(Event.COMPLETE,fn);
			sendRequest.load(urlReq);
		}
	}
}

 

<?php
/*
 * Created on 2012-6-18
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */ 
 $arr = array(); 
 $tmp = $_GET["cmd"];
 if($tmp ==null)
 {
	$tmp = $_POST["cmd"]; 	
 }
 if($tmp==null)
 {
 	$tmp = "aa";
 }
 $arr["cmd"]=$tmp; 
 $output = json_encode($arr);
 echo $output;
?>

 

转载于:https://www.cnblogs.com/liulf/archive/2012/06/18/2553878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值