Engine/RestApi

From Elgg Documentation

Jump to: navigation, search

REST-like API

Elgg has a set of functions and classes builtin to enable a plugin to expose functionality.

First, create a function which should be accessible via the API and which will handle data; for example function your_api_function(array $params). Next, you'll need to register that function with register_method(), which takes three parameters:

  • the name of the function to the outside world, like user.getEmail
  • the handler function (function your_api_function())
  • a named parameter array, where each value is an array itself containing two values:
    • the parameter name
    • a boolean indicating whether the parameter is optional or not.

Example

This registers the function api_name as the recipient of requests to the api.name API call, which takes two parameters: an integer and an optional string.

register_method("api.name", "api_name", array(
        "param1" => array(  // parameter name
                    'int'   // The type (int,bool,string etc)
                 ),
        "param2" => array(
                    'string',
                    true    // Optional parameter (default:false) specifying whether this parameter is optional.
                )
        ));

We then define the api_name function:

function api_test($params) {
 
	$int = $params[0];
	if (isset($params[1])) $string = $params[1];
 
	// Do some processing
 
	return $arbitrary_result;
 
}

Related functions

Reference/Core/register_method

Personal tools