Ok, you’ve installed Amfphp, you’ve tested that it’s up and running with the service browser, and now you’re ready to do something real with it.
We’re going to build a service called YourService, with a method called helloWorld. This method shall return a string ‘hello world from the server’.
Then we’re going to build a client that calls this service method, and displays what the server returned.
writing a service
For Amfphp a service is a class that it’s been configured to use as a service. All the methods of this class are exposed as methods of the service. So for our example, you should create a class called YourService, in a file called YourService.php, and put it in the Amfphp/Services folder. This class should contain the method helloWorld which should return the string « hello world from the server ».
Your class should look like this:
<br />class YourService {<br /><%%KEEPWHITESPACE%%> public function helloWorld(){<br /><%%KEEPWHITESPACE%%> return 'hello world from the server';<br /><%%KEEPWHITESPACE%%> }<br />}<br />
If you go to your service browser you should now see it in the available services. Try it to make sure everything works well on your server.
Now you can write the client code. This depends on which client-side technology you are using, so choose the appropriate part below.
If you get stuck, remember there is the client generator which can provide you with a full project that calls your helloWorld method.
Flash
In Flash the best way to communicate with Amfphp is with the AMF protocol and the NetConnection object. Furthermore you’ll need a function that can be called when the result is available, and a Responder object.
so here’s the function that is called:
function handleResult(result:Object):void{
trace("the server said : " + result);
}
you can then create a NetConnection object and a Responder object, connect to your server and call your service:
var netConnection:NetConnection = new NetConnection();
var responder:Responder = new Responder(handleResult, null);
netConnection.connect("http://localhost/Amfphp/index.php");
netConnection.call("YourService/helloWorld", responder);
Flex
The standard way of doing things with a Flex project involves creating an XML file containing information about your server and using it in the compiler options. There are ways to get around this, but as it’s the standard way it will be used here.
In the src folder of your project create a « services-config.xml » file. In it you can put the following:
<br /><?xml version="1.0" encoding="UTF-8"?><br /><services-config><br /><%%KEEPWHITESPACE%%> <services><br /><%%KEEPWHITESPACE%%> <service id="amfphp-flashremoting-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage"><br /><%%KEEPWHITESPACE%%> <destination id="amfphp"><br /><%%KEEPWHITESPACE%%> <channels><br /><%%KEEPWHITESPACE%%> <channel ref="amfphp"/><br /><%%KEEPWHITESPACE%%> </channels><br /><%%KEEPWHITESPACE%%> <properties><br /><%%KEEPWHITESPACE%%> <source>*</source><br /><%%KEEPWHITESPACE%%> </properties><br /><%%KEEPWHITESPACE%%> </destination><br /><%%KEEPWHITESPACE%%> </service><br /><%%KEEPWHITESPACE%%> </services><br /><%%KEEPWHITESPACE%%> <channels><br /><%%KEEPWHITESPACE%%> <channel-definition id="amfphp" class="mx.messaging.channels.AMFChannel"><br /><%%KEEPWHITESPACE%%> <endpoint uri="http://localhost/Amfphp/index.php" class="flex.messaging.endpoints.AMFEndpoint"/><br /><%%KEEPWHITESPACE%%> </channel-definition><br /><%%KEEPWHITESPACE%%> </channels><br /></services-config><br />
Note that the only elements of interest here for our purposes are the url « http://locahost/Amfphp/index.php » and the name of the destination « amfphp ».
Once you’ve got this file, add the following to the compiler options ( project properties->compiler options).
-services services-config.xml
This tells the compiler to include the information contained in the service-config.xml file you just created.
Now onto the code itself. As for Flash, you need a function that is called when the result of the call is available:
<br />function handleResult(event:ResultEvent):void{<br /><%%KEEPWHITESPACE%%> Alert.show('the server said : ' + event.result);<br />}<br />
The objects used are slightly different. Here it’s a RemoteObject that we use:
<br />var ro:RemoteObject = new RemoteObject;<br />ro.destination = 'amfphp';<br />ro.source = 'YourService';<br />ro.addEventListener(ResultEvent.RESULT, handleResult);<br />ro.helloWorld();<br />
HTML + Javascript
With HTML + Javascript the best protocol to communicate with Amfphp is JSON. This is because JSON is natively supported by Javascript, whereas AMF is not. If you would like to try AMF with Javascript, there are some available implementations, so give them a try.
So, you need a function that is called when the call returns:
<br />function onSuccess(data)<br />{<br /><%%KEEPWHITESPACE%%> alert('the server said : ' + data);<br />}<br />
and something to make an AJAX call to the server. Here we will use JQuery
You also need something to create your JSON string. You can either do it by hand or use a library to do it for you. Here we create the object describing the call and use the JSON stringify function. Finally, note that the standard JSON content type (application/json) is passed as a GET parameter to make sure the server understands that the data is JSON.
<br /><%%KEEPWHITESPACE%%> var callData = JSON.stringify({'serviceName':'YourService', 'methodName':'helloWorld'});<br /><%%KEEPWHITESPACE%%> $.post('http://locahost/Amfphp/index.php?contentType=application/json', callData, onSuccess);<br />
What’s next?
Once you can return data from Amfphp to your client, try to get some information from a database. Note that you can’t return a PHP resource directly, you have to convert it to an array!
You can find some example code in Examples/Php/ for basic database operations and authentication. Start by reading Examples/Php/readme.txt.
Hector
Where is located the examples folder?
ariels
For once the doc is in advance compared to the release … They aren’t in the alpha, but in the upcoming beta. You can grab them here though in the meantime:
https://github.com/amfphp/amfphp-2.0/zipball/master
ram
hi,i have problem with opening amfphpflashpizza .fla file in pizza example .. that shows, a unexpected file
format. can u help me to retrieve this problem….
ariels
technical questions in the forum, please.
http://sourceforge.net/projects/amfphp/forums
sabarish
please help me.
i am now work in codeigniter.
how to integrate the amfphp and codeigniter?
i was tried by
put amfphp framework in codeigniter frame work
codeigniter/amfphp.
codeigniter/amfphp/services folder my services file.
//sample1.php
//CodeIgniter.php
CodeIgniter.php and sample1.php files were put into the services folder.
but i got the error
» INVALID AMF MESSAGE »
in amfphp/browser.
Please help me………..
ADVANCE thanks.
bluehipy
-services services-config.xml in your Additional Compiler Arguments inside of Flex Builder > Project properties > Flex Compiler
hayesmaker
command line: Error: unable to open ‘services-config.xml’
I have services-config.xml in the src folder with my main application mxml file.
hayesmaker
nvm, it should be -services src/services-config.xml if you’re using FlashDevelop
Titus Barik
I believe that this line is incorrect in the example:
var responder:Responder = new Responder();
It should be instead say:
var responder:Responder = new Responder(handleResult);
Titus Barik
Also,
netConnection.connect(« http://localhost/Amfphp »);
needs to be:
netConnection.connect(« http://localhost/Amfphp/ »);
to avoid a NetConnection.Call.BadVersion. (trailing slash).
ariels
fixed!
many thanks for pointing these out
mmostafa
what does the destination and endpoint properties of the remote object refer to?
what should their values
and why there is no service-config file associated with the flex example in examples directory?
mmostafa
I am trying the code in flex but my endpoint uri is different
it is http://mydomain.com/amf/Amfphp/Services/
this is the end point i use i have changed the one in the example in services-config.xml
i have got the « NetConnection.Call.Failed » HTTP: Failed
do anyone have a solution?
Ariel Sommeria-Klein
Please use the forums for technical questions. Post there and I will gladly answer you.
https://www.silexlabs.org/amfphp/documentation/using-the-forums/
cristiangohl
Hi, using flex 4.9.1, if I set -services (service file). I got internal error.
Junior Passarelli Antunes
An Example of mysql databse would be very welcome! From 1.9 to 2.1.1 it´s been such a difficult to relearn all the stuff…
Examples and tutorials are the best of foruns! Please put some simple, Team Leaders!
In Resume… HELP!!!!
Rsrsrs…
Junior Passarelli Antunes
Just for the new users of the newest AMFPHP 2.1.1 that already used 1.9 or 2.0:
2.1.1 works well in PHP 5.4.* old versions dont work on that…
And for work with databases, forget que gateway.php… It´s dead…
In php, don´t try the old RETURN MYSQL_QUERY (SELECTED SOMETHING FROM SOMEWARE). It´s dead too!!!
Now you have to make a array with mysql_fetch_assoc with the result…
Something like this in your old Service class:
function getsomethingFromMysql($argument)
{
$resultset = array();
$result=mysql_query(« SELECT something, anotherthing from somedatabase ») or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$resultset[] = $row; // fetch each row…
}
mysql_free_result($result); // optional though…
return($resultset);
}
I take a long time researching, relearning this stuff… Hope it helps others lazy boys like me, trying the new tricks for old dogs…
Rssrsrs
Ariel Sommeria-Klein
Thank you for your comment. I’m sorry you had trouble using the latest. Just so you know, there is a page for you that contains the information you were missing, I think:
https://www.silexlabs.org/amfphp/documentation/upgrading-from-amfphp-1-9/
I do advise using PDO too instead of accessing MySQL as you do.
Junior Passarelli Antunes
Ok Ariel!
PDO Will it be!!! (Yoda talkin´! »)
Thanks!
Ivan
I am new to AMFPHP. I have Drupal7 with AMFServer insalled.
How do I call function from class in php file using Flash as3?
Where can I get Flash as3 Pizza example?
Ariel Sommeria-Klein
I suggest you read
https://www.silexlabs.org/amfphp/documentation/getting-started/
then once you have a better understanding of what AmfPHP can do for you, please use the forums
https://www.silexlabs.org/amfphp/documentation/using-the-forums/
Stephen C
Note:
For Flex section above there is a misspelling on line 17 of services-config.xml
Line 17: <endpoint uri='http://locahost/Amfphp/'
should be: localhost
Ariel Sommeria-Klein
are you sure? I just double checked, places like http://cookbooks.adobe.com/post_How_to_configure_the_services_config_xml_for_AIR_a-12034.html all mention something along the lines of http://{server.name}:{server.port}/{context.root}/messagebroker/http
Stephen C
Sorry Ariel, I should have been more clear
The actual word « localhost » in the example source code is misspelled as « locahost », it is missing the letter L
http://locahost/Amfphp/
http://localhost/Amfphp/
Ariel Sommeria-Klein
doh! ok thanks, fixed