Your first Project using Amfphp

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.


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");"YourService/helloWorld", responder);


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 />&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;services-config&gt;<br /><%%KEEPWHITESPACE%%>    &lt;services&gt;<br /><%%KEEPWHITESPACE%%>        &lt;service id="amfphp-flashremoting-service" class="" messageTypes="flex.messaging.messages.RemotingMessage"&gt;<br /><%%KEEPWHITESPACE%%>            &lt;destination id="amfphp"&gt;<br /><%%KEEPWHITESPACE%%>                &lt;channels&gt;<br /><%%KEEPWHITESPACE%%>                    &lt;channel ref="amfphp"/&gt;<br /><%%KEEPWHITESPACE%%>                &lt;/channels&gt;<br /><%%KEEPWHITESPACE%%>                &lt;properties&gt;<br /><%%KEEPWHITESPACE%%>                    &lt;source&gt;*&lt;/source&gt;<br /><%%KEEPWHITESPACE%%>                &lt;/properties&gt;<br /><%%KEEPWHITESPACE%%>            &lt;/destination&gt;<br /><%%KEEPWHITESPACE%%>        &lt;/service&gt;<br /><%%KEEPWHITESPACE%%>    &lt;/services&gt;<br /><%%KEEPWHITESPACE%%>    &lt;channels&gt;<br /><%%KEEPWHITESPACE%%>        &lt;channel-definition id="amfphp" class="mx.messaging.channels.AMFChannel"&gt;<br /><%%KEEPWHITESPACE%%>            &lt;endpoint uri="http://localhost/Amfphp/index.php" class="flex.messaging.endpoints.AMFEndpoint"/&gt;<br /><%%KEEPWHITESPACE%%>        &lt;/channel-definition&gt;<br /><%%KEEPWHITESPACE%%>    &lt;/channels&gt;<br />&lt;/services-config&gt;<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%%>'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.


  1. Hector

    8 Juin 11

    Where is located the examples folder?

  2. ram

    15 Juil 11

    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….

  3. sabarish

    6 Août 11

    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/services folder my services file.



    CodeIgniter.php and sample1.php files were put into the services folder.

    but i got the error
    in amfphp/browser.

    Please help me………..

    ADVANCE thanks.

  4. bluehipy

    6 Août 11

    -services services-config.xml in your Additional Compiler Arguments inside of Flex Builder > Project properties > Flex Compiler

  5. hayesmaker

    7 Août 11

    command line: Error: unable to open ‘services-config.xml’

    I have services-config.xml in the src folder with my main application mxml file.

  6. hayesmaker

    7 Août 11

    nvm, it should be -services src/services-config.xml if you’re using FlashDevelop

  7. Titus Barik

    26 Août 12

    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);

  8. Titus Barik

    26 Août 12

    netConnection.connect(« http://localhost/Amfphp »);

    needs to be:
    netConnection.connect(« http://localhost/Amfphp/ »);

    to avoid a NetConnection.Call.BadVersion. (trailing slash).

  9. ariels

    27 Août 12

    many thanks for pointing these out

  10. mmostafa

    5 Nov 12

    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?

  11. mmostafa

    5 Nov 12

    I am trying the code in flex but my endpoint uri is different
    it is

    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?

  12. cristiangohl

    20 Mar 13

    Hi, using flex 4.9.1, if I set -services (service file). I got internal error.

  13. Junior Passarelli Antunes

    8 Avr 13

    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!!!!

  14. Junior Passarelli Antunes

    8 Avr 13

    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…

    I take a long time researching, relearning this stuff… Hope it helps others lazy boys like me, trying the new tricks for old dogs…


  15. Ivan

    6 Mai 13

    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?

  16. Stephen C

    19 Juil 13


    For Flex section above there is a misspelling on line 17 of services-config.xml

    Line 17: <endpoint uri='http://locahost/Amfphp/&#039;

    should be: localhost

  17. Stephen C

    22 Juil 13

    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


Laissez un commentaire

News letter

Evénements Silex Labs sur Paris et sa région: Inscrivez vous à la Newsletter mensuelle

Silex Labs community Tweets

Facebook page