Creating an API Using AMFPHP


AMFPHP provides an easy way to create web-services that can be accessed by Flash and by any other programming language. Both JSON and XML returns are available making it a smart choice for any application. There are no complicated gateways to be created, just write your methods and let AMFPHP do the rest of the work.

Getting Started

The first step in getting started is downloading AMFPHP. You can do that here. The nice thing about it, is that it is drag and drop. There are no complicated settings. Simply, unzip and upload to your server in whatever location you desire.

Creating Methods

AMFPHP uses a class like structure in order to track methods, which is very useful for creating an API. For example, below we create a simple user class as we would in php. Then we create a public function because we will want to be able to access this method from outside the class. It is really simple, it just returns the name of the user given the user id.

class User{

public function getName($userID){

  return $name;

The files related to the services can be found inside the services folder. There is also a browser for you to test your code. If you navigate to /localhost/myApiFolder/browser/ you will see a Flex interface that loads all your methods and allows you to call them.

Calling Methods

Calling a method as a webservice is quite simple. There are 3 different return types available: AMF, JSON, and XML. Each of which have their own gateway. Here are some examples using our user class:

AMF – /localhost/myApiFolder/gateway.php/User.getName/1/
JSON – /localhost/myApiFolder/json.php/User.getName/1/
XML – /localhost/myApiFolder/xmlrpc.php/User.getName/1/

Each call uses the user class, method getName, and provides a user id of 1. So the generic call is /localhost/myApiFolder/gateway.php/MyClass.myMethod/param1/param2/…/paramN/. Hopefully, this has been helpful and will allow for quick setup of a webservice. AMFPHP is highly extendible and capable of much more, drop me a line if you have more questions.

Related Post