If you want to connect Flash to PHP code via AMFPHP, you need to write your PHP code as a PHP class. The remote methods that your ActionScript code calls are the methods of your custom classes. A PHP class uses the following syntax:
class MyClass { var local1 = 1; var local2 = 2; function MyClass ( ) { // do something } function someMethod ($val) { // do something else } function someOtherMethod ($val0, $val1) { // and something else } }
In this case, local1 and local2 are instance variables (unique variables associated with each instance of this class). The function MyClass( ) is this class's constructor, which runs when an instance of this class is first created. This method performs necessary setup for an instance of this class. Finally, someMethod( ) and someOtherMethod( ) are two sample methods to which all instances of this class have access.
For AMFPHP to work properly, each class must be defined in its own file and the file must have the same name (including capitalization) as the class. In addition, the class's constructor must include a method table, specific code to define the signatures and permissions of all methods of the class. This information is used primarily by the Service Browser in Flash MX to help you write the correct ActionScript, but the permission information is used by AMFPHP to limit which methods of your class are accessible via Flash Remoting. This approach lets you define private methods that can be called internally only.
An example method table is shown in Example 9-2, along with methods for the class. This code should be placed in MyClass.php.
class MyClass { function MyClass ( ) { // Create the method table for AMFPHP $this->methodTable = array( "someMethod" => array( "description" => "Retrieve the list of countries", "roles" => "list", "access" => "remote", "arguments" => array("state") ), "someOtherMethod" => array( "description" => "Retrieve a list of companies in a given country", "roles" => "list", "access" => "remote", "arguments" => array("country", "postalCode") ) ); } function someMethod ($val) { // Just echo back the value return $val } function someOtherMethod ($val0, $val1) { // and something else } }
As you can see, this code defines a variable named methodTable that is an array of all the methods this services supports, along with information about each method. The access property can be set to "remote", "public", or "private". AMFPHP closely mirrors ColdFusion in the way that method permissions are set. If you want your method accessible only to this class, define its access property as "private". If you want the method accessible to other classes, declare access as "public". Finally, if you want the method to be callable via Flash Remoting, set access to "remote". Refer to Section 5.3.3.4 for more information about the access property.
Although Version 0.5.1 of AMFPHP doesn't use the roles property, by the time you read this AMFPHP will most likely use the roles property to implement a security system similar to how ColdFusion works (see "Using Role-Based Security with ColdFusion Components" in Chapter 5 for related information). In general, if you don't plan on using the roles information, simply leave the roles property out of your code.
The two remaining properties, description and arguments, provide a description of the method and the arguments that the method accepts. At this time, the arguments property is used only for documentation purposes and won't cause an error if it doesn't match the actual arguments specified in the method declaration.
|
If there are any syntax errors in your class constructor, Flash Remoting displays an error, usually one that doesn't provide a lot of information about the problem. However, you can check for syntax errors easily by opening a web browser to the URL of your service (i.e., the URL of the .php file). Any syntax errors are displayed along with their line numbers.
If no syntax errors exist but you are still receiving an error in Flash (often the Bad Version error), make sure that AMFPHP is properly installed and that your class isn't accidentally outputting any extra characters, such a spaces, tabs, or other whitespace directly around your <?php> tag.
Once you have created a PHP class, you need to write the client-side ActionScript to connect to it. The URL you give for the gateway is just the normal URL to your gateway.php file. However, the URL you specify for your service is a bit different. Instead of being a URL delimited by slashes, it is a dotted path to your PHP class's file, starting from the directory that you specified to be the base path of your classes in your gateway.php file. For example, if you specified your classpath as:
/usr/local/apache/htdocos/frdg/services/
and your actual PHP class is located at:
/usr/local/apache/htdocos/frdg/services/stuff/MyClass
then the URL of the class would be:
stuff.MyClass
Example 9-3 shows the client-side ActionScript code to access MyClass.
#include "NetServices.as" onResult = function (result) { trace("Received result: " + result); }; gatewayURL = "http://localhost/frdg/gateway.php"; serviceURL = "stuff.MyClass"; gateway = NetServices.createGatewayConnection(gatewayURL); service = gateway.getService(serviceURL); service.someMethod(this, {testString:"Testing..."});