The goal of this interface is that one can instantiate a PHP-Class (i.e. a service) from the JavaScript side (the client). Once instantiated in javascript a mirrored object of the PHP-Instance is available. This mirrored object provides al methods the service provides. If they are called a HTTP-Request is performed that calls the corresponding method in the service-Object. The return value of this PHP-method will be the return value of the mirrored method in Javascript.
Following PHP- and JS-Components are required to use this:
Server-side (PHP):
service.php, the file accepting and interpreting the HTTP-Requestsservices.inc.php, where the services are definedcore/api_provider.inc.php, manageds and instantiates the servicescore/stdfuncs.inc.php, the general libraryClient-Seitig (JS):
scripts/core/phpremote.js, Creates the HTTP-Requests and interprets the HTTP-Response, creates the mirrored service objects.scripts/core/stdfuncs.js, the general library in javascript.A service is a PHP-Class instantiated with particular parameters. Every service must have a unique service name. The service name must not equal the php-classname, and one PHP class can be instantiated multiple times as multiple services with different parameters.
The file services.inc.php defines under which service names which PHP-classes instantiated with which parameters run.
Example content of services.inc.php
$services['timer_zeit']['module'] = "modules/timer/timer_api.inc.php";
$services['timer_zeit']['class'] = "timer_api"; // actually not needed since the classname equals the filename, otherwise mandatory
$services['timer_zeit']['params'] = array("H:i:s");
In this case a service called timer_zeit would be available.
An array of all methods this service provides can be loaded by following HTTP-Request.
service.php?method=getMethods¶meter[0]=timer_zeit
The HTTP-Response would be
["__construct", "__sleep", "__wakeup", "getTime"]
To call the method getTime following request would be needed:
service.php?service=timer_zeit&method=getTime
phpremote.js is written to abstract these HTTP-Request details and making it possible to call PHP (i.e. webservice's) methods directly from javascript. Following files are required:
<script type='text/javascript' src='scripts/core/stdfuncs.js'></script> <script type='text/javascript' src='scripts/core/phpremote.js'></script>
Afterwars following is possible:
// Instantiates the service "timer_zeit", HTTP-Request: "service.php?method=getMethods¶meter=parameter[0]=timer_zeit
// timerApi will be your mirrored service object
var timerApi = services.getApi('timer_zeit')
// Calls the getTime method the service provides, HTTP-Request: service.php?service=timer_zeit&method=getTime
var zeit = timerApi.getTime();
After importing phpremote.js, services is a global available object in javascript.