Adobe ColdFusion Box
There is a new and shiny AIR application for those who are interested in ColdFusion and live in Europe: ColdFusion Box.
Quoting from the site:
”Adobe ColdFusion Box is your central point of information about ColdFusion news, events and activities in Europe, Middle-East and Africa.
Use this application to learn more from our white papers, datasheets or study guides as well as keeping you informed about what’s fresh around ColdFusion at Adobe.”
Enjoy!
New version of Tour de Flex
A new version of the Tour de Flex AIR application is available, and now there is a web version too: http://www.adobe.com/devnet/flex/tourdeflex/web.
Some of the changes are: improved skin with thinner chrome, a new start screen, a fix for a memory/CPU issue…
You can find more about the changes on Greg Wilson’s website.
Flash applications and Facebook
Facebook and Adobe released an ActionScript 3 client library for the Facebook Platform API, to make it easier to build Flash applications for the Facebook platform.
You can find additional information on this library here (a video tutorial by Daniel Dura), and there is a new section, dedicated to this subject, on Adobe Developer Connection. And here is the project page, where you can download it.
Enjoy!
Flex and remoting with PHP, which library is the best: Zend AMF, AMFPHP, WebORB for PHP, or SabreAMF?
You’ve decided to go for remoting as the way to communicate with your PHP server, and you’re wondering which of these four libraries is the best: Zend AMF, AMFPHP, WebORB for PHP, and SabreAMF?
Well, it will probably surprise you, but I don’t think one is necessarily better than another. There are some small differences, but for me none is so big that I can declare one as the winner. It depends on your specific needs, and the context of your current workflow with PHP apps. And I will explain in a minute why that is.
Performance
For me performance is one of the top criteria that I would use to choose one over another. All these libraries are written in PHP, and although I didn’t conduct an extensive test, I don’t think consider one faster than another. If you wrote a PHP module in C, to add some new API to PHP for remoting, then you could have something much faster. For example, AmFast (a remoting library for Python written in C) is up to 18x faster than PyAMF written in Python.
As the situation stands today, I don’t think we have a winner here. If someone performs a test and finds that I am wrong, please let me know!
Documentation and ease of use
Here, the alternatives are not so level. While implementing examples with each one of them, I must say AMFPHP was the easiest to use. The documentation is OK, since it has been around for some time you can find articles explaining how to use it; and it has a Services Browser, a Flex application that lets you test your PHP services. This can be very handy when you are debugging a project. There is a folder where you can drop your service and it will be automatically picked up, and a folder for your VOs. These two folders can be configured.
WebORB has good documentation too, and it comes with examples. There is also a Flex application that provides easy access to some quick start articles, a browser service, a code generator, and a way to configure the destinations. I didn’t find this code generator particularly helpful. For example, I expected to generate an ActionScript class out of the PHP model class, instead it generated a wrapper class for the remote object, and it tried to generate the ActionScript model. I say it tried to generate, because it generates a strange data model, which basically has a property for each method on your PHP remote class of Object type. It has a special folder where you have to add your services and VOs.
The next one is Zend AMF. It has decent documentation and is the newest on the market. The guy who developed it, actually is the same guy who created AMFPHP, so it must be good. However it doesn’t have a service browser, so it is harder to debug the PHP code compared to the previous two. But it has a big advantage: it is part of Zend Framework. So if you are already using Zend Framework, or you plan to use features from it, then this is the choice for you. You need to create a bootstrap file to start the AMF server, to add the services (either one by one, or by folder), and to set the correspondence between the ActionScript and PHP data model classes.
The last one is SabreAMF. The documentation is not so good and there are not so many articles, but if you know what remoting is, then you can find your way. It is little bit strange to use, as it doesn’t impose any particular structure. You have to create a boot strap file, where you configure the corresponding data model classes. And you need to create a callback function that will do the lookup for your service classes. Actually, if you change the callback function a little bit, you can even call your defined functions, not only object’s methods.
Footprint
The smallest ones are SabreAMF and AMFPHP. These two just do remoting. Zend AMF is part of Zend Framework, an application framework for PHP (so you have a lot of functionality, and a lot of code). WebORB for PHP does remoting, plus messaging and data management. So again, there is more code in it.
Future
It seems all four libraries are maintained, although two of them have companies behind them: Zend AMF with Zend Technologies, and WebORB for PHP with Midnight Coders. The other two have one smart developer behind them. The thing is that it doesn’t really matter who is behind it, because it is really easy to switch from one framework to another one. Thus, if in the future you find that your PHP library for remoting is no longer maintained, it is not the end of the world; you can switch to another one.
Mirror, mirror on the wall, who is the most beautiful of them all?
Instead of spending too much time deciding which one to choose, just play with it or look at an example and see for yourself which one you are in tune with. If you have some special needs, for example, if you are already using Zend Framework, or using another framework, then it is even easier to choose.
As I already said, even if you eventually find that you made the wrong choice, it is not the end of the world, you can switch to another one very easily.
Happy remoting, people!
PS. If you have a favorite, do you mind posting a comment to briefly explain why you chose it? Thanks!
Flex, Adobe AIR, and PHP in Gdansk, Poland
Last Saturday I had the pleasure to do an event together with my colleague, Piotr, in Gdansk. He has pictures and a short video we shot before the event; click here.
I must say, I enjoyed the event a lot (probably it was the most stylish and modern venue I ever seend for an event :)), and the city is beautiful.
Flex and PHP: remoting with SabreAMF
In the past months I have written a lot about Flex and PHP remoting using AMFPHP, ZendAMF, and WebORB. However, there is another library, SabreAMF, that can be used to do remoting. This article explains how to use it.
If you don’t know what remoting is (or AMF), then it might be a good idea to read my earlier post first (look for the “What is AMF and remoting and why should you use it” section).
Installing SabreAMF
You can download the library from here, and here you can find some documentation. I will say upfront, that indeed, compared to the other three PHP libraries for remoting, SabreAMF offers less documentation.
Unzip the archive somewhere on your hard-disk. You have two alternatives for using it with your PHP code:
- You can add the library to your PHP include_path in your php.ini file. To do this you need to create a folder, called for example “mysabre”, and in this folder create a folder called “SabreAMF”. In this last folder copy all the files from the archive (AMF0, AMF3, examples, and so on). Next, open the php.ini file and search for the line include_path and add the absolute path to the folder mysabre. On my machine, this looks like this:
include_path = “/frameworks/mysabre” - Alternatively, you can create a folder called “SabreAMF” inside of your PHP working directory, and copy to this folder all the files and folders from the archive.
You are now ready to move to the PHP code you need for this example.
Create the Flex project
Usually, when I work with Flex and PHP projects, I prefer to use Flex Builder and Zend Studio installed together. It is possible, however, to work with Flex Builder and a PHP plugin to help you with the PHP code. Either way, you should create a Flex project that uses PHP on the server side (if you plan to use Zend Studio and Flex Builder, first create a Zend PHP Project, then use the Add Flex Nature wizard to add Flex PHP nature on the project). This way you streamline the deployment of the SWF file (the compiled result of the Flex project) to the PHP server. I chose to create a new project called “flex_sabre”.
Next, create a folder inside the PHP server root named “sabreamf_remoting”, and add this folder to the project. Choose New > Folder, and then click on the Advanced button.
Create the PHP code
The example will use a PHP class to manage one table from a MySQL server.
In the “sabreamf_remoting” folder, create three PHP files: MyService.php, VOAuthor.php, and index.php. Open the MyService.php page and paste the following code (you need to update the connection information for your specific database setup; to do this, look for the four constants at the top of the class):
1: <?php
2: require_once 'VOAuthor.php';
3:
4: //conection info
5: define("DATABASE_SERVER", "localhost");
6: define("DATABASE_USERNAME", "mihai");
7: define("DATABASE_PASSWORD", "mihai");
8: define("DATABASE_NAME", "flex360");
9:
10: //$o = new MyService();
11: //print_r($o->getData());
12:
13: class MyService {
14:
15: /**
16: * Retrieve all the records from the table
17: * @return an array of VOAuthor
18: */
19: public function getData() {
20: //connect to the database.
21: //we could have used an abstracting layer for connecting to the database.
22: //for the sake of simplicity, I choose not to.
23: $mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
24: mysql_select_db(DATABASE_NAME);
25: //retrieve all rows
26: $query = "SELECT id_aut, fname_aut, lname_aut FROM authors_aut ORDER BY fname_aut";
27: $result = mysql_query($query);
28: //throw (new Zend_Amf_Exception('eroare ca asa vreau eu', 11));
29: $ret = array();
30: while ($row = mysql_fetch_object($result, "VOAuthor")) {
31: $ret[] = $row;
32: }
33: mysql_free_result($result);
34: return $ret;
35: }
36: /**
37: * Update one item in the table
38: * @param VOAuthor to be updated
39: * @return NULL
40: */
41: public function saveData($author) {
42: if ($author == NULL)
43: return NULL;
44: //connect to the database.
45: $mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
46: mysql_select_db(DATABASE_NAME);
47: if ($author->id_aut > 0) {
48: //save changes
49: $query = "UPDATE authors_aut SET fname_aut='".$author->fname_aut."', lname_aut='".$author->lname_aut."' WHERE id_aut=". $author->id_aut;
50: } else {
51: //add new record
52: $query = "INSERT INTO authors_aut (fname_aut, lname_aut) VALUES ('".$author->fname_aut."', '".$author->lname_aut."')";
53: }
54: $result = mysql_query($query);
55: return NULL;
56: }
57:
58: public function deleteData($author) {
59: if ($author == NULL)
60: return NULL;
61:
62: //connect to the database.
63: $mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
64: mysql_select_db(DATABASE_NAME);
65: //delete a record
66: $query = "DELETE FROM authors_aut WHERE id_aut = ".$author->id_aut;
67: $result = mysql_query($query);
68: return NULL;
69: }
70:
71: public function setCredentials($user, $password, $charset) {
72: return true;
73: }
74: }
75:
76: ?>
This is the class you will call from Flex. It has three methods: one to get all the records from the table, another to update the values for one record, and the third to delete a record.
Let’s create the code for the Value Object, the data model. This is used by the MyService class to wrap one row from the table. Thus, the method getData() returns an array of VOAuthor, and the method saveData() receives one argument: the VOAuthor of the row that was changed. Open the file VOAuthor.php and add this code:
1: <?php
2: class VOAuthor {
3: public var $id_aut;
4: public var $fname_aut;
5: public var $lname_aut;
6: }
7: ?>
As you can see, this class is very simple; it just provides the same members as the fields from the table. Finally let’s create the code for index.php file. This is the plumbing code that exposes the MyService class to Flex clients with the help of SabreAMF. Add the following code:
1: <?php
2: require_once 'SabreAMF/CallbackServer.php';
3: require_once 'MyService.php';
4: require_once 'VOAuthor.php';
5:
6: function myCallback($serviceName, $methodName, $arguments) {
7: // You can use the servicename to lookup and instantiate a service class
8: $serviceObject = new $serviceName;
9: if (!is_array( $arguments)) {
10: $arguments = array($arguments);
11: }
12: // check the php manual if you don't know what this function does
13: return call_user_func_array(array($serviceObject, $methodName), $arguments);
14: }
15:
16: //map the AS VO to PHP VO
17: SabreAMF_ClassMapper::registerClass('org.corlan.VOAuthor','VOAuthor');
18:
19:
20: // First we'll create the server object
21: $server = new SabreAMF_CallbackServer();
22: // setup the callback
23: $server->onInvokeService = 'myCallback';
24: // parse the request and spit out the response
25: $server->exec();
26: ?>
And this is the part where you have to pay attention. Chances are that if you encounter problems later, the root of the problems is in this file. First of all, you use an instance of SabreAMF_CallbackServer to set up the server that knows how to handle the remoting calls from Flex. Next, you add a PHP function as the callback for the onInvokeService property of the SabreAMF_CallbackServer. This function will be called each time the PHP server receives a remote call. Next, you define the callback function. In my code, the function is called myCallback.
You might wonder why you need to do this, and why it is not done automatically for you by SabreAMF. Indeed, it would have been very easy to have a default function registered for you, so you don’t have to do this copy & paste step. However, there is a reason: you can write your own function and you can handle different calls in different ways. In other words, it is easy to customize how a specific remote call should be handled by your code. If you are not satisfied with this answer, please let me hear your opinion :D
And finally, you use the static method registerClass(‘package.remoteclass,’localclass’) on SabreAMF_ClassMapper object to tell PHP which ActionScript VO corresponds to the PHP VO.
And now, you are ready to move to the client code.
Creating the Flex application
This is very simple code. You have the MXML file with a RemoteObject to do the calls to the PHP class. Then you have a data grid to display and edit the existing records, and two buttons: one for save and one to get the data. Next there are a bunch of functions, some are listeners for various events (result event or fault event on the remote object, change item on the data grid). Here is the code:
1: <?xml version="1.0" encoding="utf-8"?>
2: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
3: creationComplete="init()" >
4: <mx:Script>
5: <![CDATA[
6: import mx.controls.Alert;
7: import mx.rpc.events.FaultEvent;
8: import org.corlan.VOAuthor;
9: import mx.events.DataGridEvent;
10: import mx.rpc.events.ResultEvent;
11:
12: private var item:VOAuthor;
13:
14: private function init():void {
15: myRPC.getData();
16: }
17: private function getDataListener(event:ResultEvent):void {
18: dg.dataProvider = event.result;
19: }
20: private function editListener(event:DataGridEvent):void {
21: item = event.itemRenderer.data as VOAuthor;
22: }
23: private function saveDataListener(event:ResultEvent):void {
24: item = null;
25: init();
26: }
27: private function saveData():void {
28: if (item == null)
29: return;
30: myRPC.saveData(item);
31: }
32: private function faultListener(event:FaultEvent):void {
33: Alert.show(event.fault.faultString);
34: }
35:
36: ]]>
37: </mx:Script>
38:
39: <mx:RemoteObject id="myRPC" destination="MyService" endpoint="http://localhost/sabreamf_remoting/index.php" source="MyService" fault="faultListener(event)">
40: <mx:method name="getData" result="getDataListener(event)"/>
41: <mx:method name="saveData" result="saveDataListener(event)"/>
42: </mx:RemoteObject>
43: <mx:VBox>
44: <mx:DataGrid id="dg" editable="true" itemEditEnd="editListener(event)" />
45: <mx:Button label="Save" click="saveData()" />
46: <mx:Button label="Get Data" click="init()" />
47: </mx:VBox>
48:
49: </mx:Application>
And finally, you have to create the ActionScript Value Object, org.corlan.VOAuthor:
1: package org.corlan
2: {
3: [Bindable]
4: [RemoteClass(alias="org.corlan.VOAuthor")]
5: public class VOAuthor
6: {
7: public var id_aut:int;
8: public var fname_aut:String;
9: public var lname_aut:String;
10: }
11: }
With this object, you must pay a little bit of attention. Normally, the value for the alias attribute should be VOAuthor (as the PHP class doesn’t have a package name). However, if you want receive typed objects in Flex and not anonymous objects, you have to put the same value as the fully qualified class name itself (and this is the value you set up in the index.php file too).
Conclusion
That’s it. You can download the project from here. I will write an article soon that compares the four available libraries for PHP and Flex remoting.
MAX 2009 in Los Angeles and Online
It has been decided that this year Adobe’s biggest conference, MAX, will be held only in Los Angeles, USA. But we didn’t forget our communities from other continents, so we are working hard to make it as easy as possible for anyone to watch it online.
The event will take place October 4th – 7th, and you can submit sessions and labs here. I strongly encourage you to submit there; it is up to us to get quality content and interesting topics.
See you there!
Oporto, Portugal sessions
| April 27, 2009 | to | April 30, 2009 |
I will travel to Oporto and Lisbon, Portugal April 27th – 29th giving sessions on Flash Platform and Flex and Adobe AIR integration with PHP.
New articles on Adobe.com
There are some new and interesting articles on Adobe.com:
- AdvancED Flex 3 excerpt: Integrating via data and media services
- Reducing the risk of malicious web attacks with HP SWFScan
- Advancing mobile storytelling with the Adobe distributable player solution
- Exploring the new 3D features in Flash CS4 Professional
Berlin and Frankfurt
Last week, I had the pleasure of presenting sessions on Flex/AIR and PHP in Berlin and Frankfurt.
In Berlin, some of the attendees and the local Flex UG managers stayed for a beer after the session. I had a chance to talk about their future plans, and finding ways to present Flex and AIR to the local students. It seems that together we could do some interesting things in the future. I will not disclose what they are planning, but they are working on a cool, big event. I’ll keep my fingers crossed, Bettina and Mathias!
In Frankfurt, I had the first session at DevDusk event (thank you guys for giving me the first slot). Although I had to squeeze into 45 minutes a session that I usually do in about 70-80 minutes, it was cool. The second session was on Groovy. Here you can see some pictures from the event. The guys behind the DevDusk event are also behind the Rhein Main Flex User group. If you are living in the area, then give it a try. Both Darren and Reto are vey nice!



PlayBook & AIR
PHP & Flex