4. Secure Service
Now let's make another example. We will make a secure service, this mean a service which needs authentication before being consumed.
And we will return a custom object, not a regular string or integer..
Always, in the flashservices/services folder create a new php class file "SendObject.php"
<?php
class SendObject
{
function SendObject()
{
$this->methodTable = array(
"getPerson" => array(
"description" => "Returns an instance of Person (Custom Class)",
"access" => "remote", // available values are private, public, remote
"roles" => "admin",
"returns" => "Person" // name of Custom Class
)
);
}
// return a Peron Object
function getPerson()
{
$person->name = "Alessandro";
$person->surname = "Crugnola";
$person->age = 29;
$person->city = "Gavirate";
$person->country = "Italy";
$person->favourites_artists = array("Tristania", "After Forever", "Nightwish", "The Sins of thy Beloved");
return $person;
}
// This function will authenticate the client
// before return the value of method call
function _authenticate($user, $pass){
if($user == "admin" && $pass = "secret"){
return "admin";
} else {
return false;
}
}
}
?>
Now try to call this method from flash as we saw earlier:
#include "NetServices.as"
#include "NetDebug.as"
mainResponder = new Object();
mainResponder.onResult = function( data ){
trace("onResult")
for(var a in data){
trace(a + ": " + data[a])
}
}
mainResponder.onStatus = function ( data ) {
trace("an error occurred")
trace("in line: " + data.line)
trace("error level: " + data.level)
trace("description: " + data.description)
}
NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway.php");
conn = NetServices.createGatewayConnection();
serv = conn.getService("SendObject", mainResponder);
serv.getPerson();
You will receive an error from the server, like this
This is because inside the methodTable we have defined the "getPerson" attribute "roles". Once defined this attribute the php class will invoke the _authenticate automatically before return the value of the method called.
If the Flash client send the correct user and pass information, the php class will return correctly the value of the method called, otherwise an error is raised.
In order to gain access to the secure service Flash needs to send some extra headers every time call a method. You need to write this piece of code before all the calls to the remote server functions:
// send the credentials request header
conn.setCredentials("admin", "secret");
In this way, the complete code to make the secure call is this:
#include "NetServices.as"
#include "NetDebug.as"
mainResponder = new Object();
mainResponder.onResult = function( data ){
trace("onResult")
for(var a in data){
trace(a + ": " + data[a])
}
}
mainResponder.onStatus = function ( data ) {
trace("an error occurred")
trace("in line: " + data.line)
trace("error level: " + data.level)
trace("description: " + data.description)
}
NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway.php");
conn = NetServices.createGatewayConnection();
serv = conn.getService("SendObject", mainResponder);
// send the credentials request header
conn.setCredentials("admin", "secret");
serv.getPerson();
Now you will see the correct results. In fact in our NetConnection Debugger panel we can see this: (remember that in php we defined as result object a "Person" object)
This is what Flash send as header for the secure connection:
And this is the result object we get:
Flash recognize the result object as a "Object" instance with some variables inside, and an array of strings.
It's important to remember that inside the methodTable array you must define a custom "return" if you want to return to flash a custom object which flash can recognize. For example: date, xml...
4. Conclusion
There is also a mailing list dedicated to AMFPHP, you can find it at:
http://sourceforge.net/mail/?group_id=72483
A forum dedicated to AMFPHP is also at this address:
http://www.sephiroth.it/...viewforum.php&f=8
Other tutorials at:
http://www.macromedia.com/.../amfphp.html
http://www.actionscript.org/tutorials/.../index.shtml
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/amfphp
This always has the latest releases during the developement. For exampe, my flashservices folder is the one downloaded from this CVS repository.
