Flash remoting with xaraya flashservices
I wanted to create some flash applications that interacts with my database. As usual, I am using Xaraya for my framework.
I knew there's flash remoting for PHP called AMFPHP. As in the documentaion, its said that AMFPHP able to be run as standalone or integrated with other framework.
So for this, I wanted to try to integrate with Xaraya. A quick search on xaraya's extension, there's a flashservices module for xaraya.
According to the flashservices module description, it is also using AMFPHP. The module, allows flash application to access data remotely using AMF (actionscript message format).
However, again, I couldn't find any documentation on how to use the module.
Spent almost a day to figure out how to use the module. Though I manage to create my flash calls the module's remoting service, I'm still not sure whether this is the best way to do.
Anyway, this is how I done that. If anyone had experience on this module, do let me know.
Tools
* Flash 8 or other flash ide like flex builder
Notes
* For Flash 8 development you need to install flash remoting components from: [http://www.adobe.com/products/flashremoting/downloads/components/ Adobe]
* Once you installed, you will see the components from Window -> Common Libraries -> Remoting
Action Scripting
Enabling flash remoting in AS
* import all the remoting components
//import remoting components import mx.remoting.*; import mx.rpc.*; //import and initialise netdebugger import mx.remoting.debug.NetDebug; NetDebug.initialize();
Setting the service
//set the gateway for flash remoting var gatewayUrl:String = "http://localhost/xaraya/ws.php?type=flashremoting"; //set service var _service:Service = new Service(gatewayUrl, null, 'xarServices', null , null);
- The 'xarServices' refers to the modules/flashservices/services/xarServices.php
- The xarServices has two method currently: xarModApiService and xarModFuncService
- xarModApiService method will call the xaraya's xarModAPIFunc function
- xarModFuncService method will call the xaraya's xarModFunc function
Calling the service's method
//set the arguments to call for the module to call //this will call flashservices_userapi_test var myargs = new Array(); myargs[0] = "flashservices"; myargs[1] = "user"; myargs[2] = "test"; //the parameters set to the function called above //notice that object is used as we might need to use the key for the function var parameters = new Object(); parameters.message = "Welcomeeeeeeeeeee!"; myargs[3] = parameters; //call the service //you can call xaraya service also by using xarModFuncService instead of xarModAPIService var pc:PendingCall = _service.xarModApiService(myargs);
- myargs is an array containing the function to be called. In this case will call module/flashservices/userapi/flashservices_userapi_test.php
- parameters is the arguments to be passed into the function
Handling flashservice return module
//do the respon processing pc.responder = new RelayResponder(this, "handleResult", "handleError");
handleResult is a function in the action script that will handle the event once the service method is successfully called.
function handleResult(re:ResultEvent) { //will set the flash label text's property to the result string mylabel.text = re.result; }
handleError is an error handler function in the action script that will handle errors returned from the service.
function handleError(fe:FaultEvent) { trace('There has been an error ' + fe.fault.faultstring); }
Flashservice module
Now, this is where I'm not sure the correct way, I had to change the following to make my flash remoting work:
- Edit modules/flashservices/classes/app/Executive.php somewhere in line 340, change the:
return call_user_func_array ( array(&$this->_classConstruct, $this->_methodname), $a); // do the magic
to
return call_user_func_array ( array(&$this->_classConstruct, $this->_methodname), $a[0]); // do the magic
this is because $a will contain multidimensional array, so need to set the first element of $a
Debugging Tips
* in actionscript you can use 'trace' command to show to the debugger, example in handleError:
trace('There has been an error ' + fe.fault.faultstring);
* in xaraya's php function, you can use 'trigger_error' function so that the error will be returned to the flash's actionscript. example:
trigger_error("There's an error!",E_USER_ERROR);
* in debugging, ensure you can allow the http access, a securitty pop up will usually show if you open the html file directly (not from http address). click on settings to manage this.
** go to website privacy settings
** click on your website address
** change to allow then restart the browser

That's all. Hopefully I can see more Flash with Xaraya applications in future....
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments
No comments yet.
Leave a comment