Translations of this page:

Das PHP/JS-Interface

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-Requests
  • services.inc.php, where the services are defined
  • core/api_provider.inc.php, manageds and instantiates the services
  • core/stdfuncs.inc.php, the general library

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

What is a (Web-)Service

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&parameter[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

AJAX-Call of a service using phpremote.js

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&parameter=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.

 
en/dev/coreall/interface.txt · Last modified: 11.12.2007 10:25 by kaegi
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki