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;
?>