Original article: http://bbs.9ria.com/thread-5157-1-1.html
Today is a lot of time to writeTutorialTo show you how to use PHPDevelopmentAMF server...
We do not need to start from scratch to support the AMF protocol. Of course, if you understand the soap support of PHP, you can also do it by yourself.AmfphpTo develop the AMF server.
First download amfphp address is http://sourceforge.net/project/showfiles.PHP? Group_id = 72483 # files
Amfphp official website address is http://www.amfphp.org/
Download the downloadedFileThe basic directory structure is
Note the Services Directory, which is placed on the developed server.CodeYou can develop a single file service to put it directly under this directory, or you can create a directory to store your server code.
The directory browser is the tool directory for debugging our AMF server code (amfphp/Browser/index.Html.
Note that this article discusses the latest amf3 protocol of the AMF protocol and the version of amfphp running on php5.x or later.
Suppose you create a service file terry_services.php and use this file name as the class name. (Note that all services must be written as classes and the same as the file name, otherwise the service will not be available)
- <? PHP
- Class terry_services
- {
- Function dosmth ($ OBJ)
- {
- Return 'services return string ';
- }
- }
- ?>
Copy code
Then we access amfphp/Browser/index.html (for the first visit, we need to set the AMF protocol. Select afm3 here and click OK .)
For example, we can see that the "service object" of terry_services is added to the service list on the left. This service object has a method "dosmth", and this method has a parameter "OBJ ".
We can write data in the parameter field.DataAnd click "call"ButtonTo call the method of the service object. For example
We can see thatEffectThe returned results can be seen in the label "Results", which is exactly the returned results written in our PHP code.
Here, we will introduce the corresponding data information in several tabs.
Info: Call execution status (call execution time and other information)
Results: returned results
Tree: displays the result in a tree.
Recordset view: DataSet result observer
Trace: debug information returned to the observer
We continue to modify the PHP code, add several input parameters to the function, and return an array to see what it looks like.
- <? PHP
- Class terry_services
- {
- Function dosmth ($ OBJ, $ param2, $ param3)
- {
- $ Return_array = array ();
- $ Return_array ['get _ OBJ '] = $ OBJ;
- $ Return_array ['get _ p2'] = $ param2;
- $ Return_array ['get _ P3 '] = $ param3;
- Return $ return_array;
- }
- }
- ?>
Copy code
Click the refresh icon button to refresh and then see the effect (not the browser refresh button)
Three parameters and corresponding input boxes are displayed.
Enter some data separately, and perform the following operations:
Okay. Have you seen the returned results of the array? Is an as object, which can be used as an array or an object in as3. Let's look at what is displayed in the tree label.
The reader should be clear about the use of this tool and the writing of the server code. The development mode of the specific server is not described here. You need to write it later...
You may have a question: if you want to pass an object, how can you use the tool for debugging?
I changed the method "dosmth" of the service object to a single parameter structure at the beginning.
- <? PHP
- Class terry_services
- {
- Function dosmth ($ OBJ)
- {
- Return $ OBJ;
- }
- }
- ?>
Copy code
This Code shows that the object is directly returned to the client. I want to tell you how to use this tool to debug incoming objects.
Here you need to know the concept "JSON". For details, you can check the information.JavascriptThe method of stringizing the object structure in
For example, a JavaScript array:
A ['one'] = 'onev ';
A ['two'] = 2;
The result after this array is serialized is {"one": "onev", "two": 2}, which is represented by braces, double quotation marks, and colons, after being passed and restored.
The debugging tool supports passing parameters in JSON format. It is automatically parsed into an array object and passed to the server.
Okay ~~ The server also knows how to write the parameters, how to pass in the parameters, and how to debug the object. The last step is how to communicate with the PHP code on the server side by using the Atom Force protocol on the client.
In as3, there is a class of netconnection, which supports the AMF protocol, and in as3, it supports the amf3 protocol.
It mainly uses its connect method and call method.
The parameter structure is as follows:
Netconnection. Connect (service address)
Netconnection. Call ('directory. Object. Method name', callback object, parameter, [parameter 2], [parameter 3]...)
Here, the call parameters starting from the second parameter are all the methods for passing the service object to the server.
See the following client code:
- VaR callback = new object ();
- Callback. onresult = function (return_value: Object)
- {
- //...
- }
- VaR _ NC: netconnection = new netconnection ();
- _ NC. Connect ('HTTP: // www.example.com/amfphp/gateway.php ');
- _ NC. Call ('terry _ services. dosmth ', callback, Param );
Copy code
Note that connect is connected to Gateway. php under amfphp (this is fixed)
The specific service object and method used are specified by the first parameter in the call. If your service object is in a deeper directory, the depth is described by. (point ).
If the directory path of terry_services.php is amfphp/services/Terry/terry_services.php, the first parameter of call here is 'terry. terry_services.dosmth'
The second parameter of call is the netconnection callback object after the server returns data. The callback object must have a fixed onresult (Param: Object) method. In this method, you can process the data returned by the server...
Basically, this is a development process. I will not attachSource code(Even if Flash is not installed in this book )...