Flash Remoting for PHP – Part 2 – Creating my own Service in amfphp (getting data)
After we got the installation working of amfphp we are now going one step further …if you missed it, find it here!
What will we need?
- correct installation of the amfphp service (0n webservice or locally)
- a php editor (personal using Adobe Dreamweaver)
- if using a amfphp installation on a online webserver, a ftp program (using FileZilla)
What is a amfphp service?
Well an amfphp service, is actually a php class that contains one or more function that handle the retrieval of database data
Example files
For a good understanding and following of this tutorial i created folowing database: designz_labs_amfphp with one table: books

Database and Table structure
for this tutorial I recommend using a local verison of amfphp using Mamp (Mac) of Wamp (Windows).
You can download the database file here , so you can follow along with the same information.
Creating the BookService
Now we will create a service that will retrieve all the books in the tabel books
Different steps:
- importing the database and creating a new database user with the right right
- opening the php editor and creating a new php file
- creating a new class BookService
- creating a basic constructor holding the connection details
- creating a new function (method) : getBooks
- calling the php function inside flash using amfPHP
Importing and User creation
importing : on the phpMyAdmin main page you will be able to import a database, do this and according to the .zip file download from above link.
user creation: on the same main page , go to Rights or Privileges and create a new user (user: books , password: books , Host:localhost) and make sure that the user has select and insert rights. This account information will be needed to have the tight privileges on the database table.
Creating a new class: BookService
open up a php editor and create a new php file.
Now create a new class: BookService

Creating the BookService
Creating the basic constructor with database connection data
Now we will add a basic constructor, where we will set connection data (telling the php that we are connection to localhost, with user: books having the password: books) and select database: designz_labs_amfphp for connection.

Creating the Basic Constructor
Creating a new function getBooks
In this peace of php we will add a new function “getBooks” , we select every record in the table “books” , and add all these records to an array … after completion the array will be returned to the amfphp , which will be called in Flash.
Finally save this php file into the amfphp service directory… Now we are ready to call this function from flash and process the results!

getBooks Service
Calling the getBooks function from Flash Actionscript

Calling the getBooks function
We just make a new netconnection and a new responder (that will cary the answer = the database result data) with the answer function when completed.
Via the netconnection we connect to the url of our amfphp (this can be local or on the network), than we call the php file BookService and the function getBooks in that php file … giving it the responder as paramater.
When the function is called with result , the getBooksResult will be called , here we loop through each object in the result array, in this case all the titles of the books ….
Download the source fla here…!
Enjoy … the next tutorial on amfphp will be calling database action combined with custom parameters !
2 comments so far
Leave a reply
[...] Flash Remoting for PHP – Part 2 – Creating my own Service (getting data) [...]
Five Starts men! Thanks a lot!!
I’ve learn a lot with your text.
Regards