Testing your AMFPHP installation

Now that you’ve installed AMFPHP, and checked that the gateway.php responds, it’s a good idea to test if it is working correctly with a sample service and movie.

Sample service

Save this file as HelloWorld.php in the /services folder:

<?php
class HelloWorld
{
    function HelloWorld()
    {
        $this->methodTable = array
        (
            "say" => array
            (
                "access" => "remote",
                "description" => "Pings back a message"
            )
        );
    }
 
    function say($sMessage)
    {
        return 'You said: ' . $sMessage;
    }
}
?>

Sample test file

Create a new Flash movie. Go into Windows > Others > Libraries > Remoting and drag and drop both the RemotingClasses and RemotingDebugClasses into your movie. Now copy and paste the following code on the first frame of the main timeline:

import mx.remoting.*;
import mx.rpc.*;
import mx.remoting.debug.NetDebug;
 
var gatewayUrl:String = "http://localhost/flashservices/gateway.php"
 
NetDebug.initialize();
var _service:Service = new Service(gatewayUrl, null, 'HelloWorld', null , null);
var pc:PendingCall = _service.say("Hello world!");
pc.responder = new RelayResponder(this, "handleResult", "handleError");
 
function handleResult(re:ResultEvent)
{
	trace('The result is: ' + re.result);
}
 
function handleError(fe:FaultEvent)
{
	trace('There has been an error');
}

Expected results

When testing in the Flash IDE, you should see the following in your trace window:

The result is: You said: Hello world!
    

In addition, you should see the following in the NetConnection debugger:

Compilation errors

If you receive an error like:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 8: The class 'Service' could not be loaded.
     var _service:Service = new Service(gatewayUrl, null, 'HelloWorld', null , null);

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 9: The class 'PendingCall' could not be loaded.
     var pc:PendingCall = _service.say("Hello world!");

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 12: The class 'ResultEvent' could not be loaded.
     function handleResult(re:ResultEvent)

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 17: The class 'FaultEvent' could not be loaded.
     function handleError(fe:FaultEvent)

Total ActionScript Errors: 4 	 Reported Errors: 4
    

It is because you have not properly included the Remoting classes in the movie. You can do so by clicking Window > Other Panels > Common Libraries > Remoting, drag and drop both RemotingClasses and RemotingDebugClasses on the stage, then delete them. This should place a copy of the files in your library. You can double-check that they are included in the library (Ctrl+L).

Runtime errors

If you receive an error such as:

Error opening URL "http://localhost/flashservices/gateway.php"
      

It is either because you have not set the gatewayUrl variable properly or your server is not started.

If you receive instead:

There has been an error
    

Then you can find out the root of this error in the NetConnection debugger. Look at the last ‘Status entry’. If it looks like this:

Status (object #2)
.....description: "The class {HelloWorld} could not be constructed, 
check the stack trace for the root cause"
.....details: "c:\pat\projets\amfphp\src\actions\ClassLoaderAction.php"
.....level: "User Error"
.....line: 61
.....exceptionStack (object #3)
..........[0] (object #4)
...............[0] (object #5)
....................code: 256
....................description: "The class {HelloWorld} could not be 
constructed, check the stack trace for the root cause"
....................details: 
"c:\pat\projets\amfphp\src\actions\ClassLoaderAction.php"
....................level: "User Error"
....................line: 61
..........[1] (object #6)
...............[0] (object #7)
....................code: 256
....................description: "The class {HelloWorld} could not be 
loaded.  The class file exists but may contain syntax errors, or the 
class is misnamed."
....................details: 
"c:\pat\projets\amfphp\src\actions\ClassLoaderAction.php"
....................level: "User Error"
....................line: 83
    

That means AMFPHP can’t find your class. Either you did not properly create the file in the services folder, or you did not set the base class path in the appropriate location in gateway.php.

Finally, another error that might pop up in the NetConnection debugger is the dreaded NetConnection.Call.BadVersion. In that case you will want to open the errors.log file and look at the last PHP error that was output and try to figure it out. If it still does not work for you then consider sending a post on the AMFPHP forums.


� amfphp.org | Disclaimer | Conditions of use