Results tagged “web services”

Elgg provides an API for building custom web services. You expose functionality through the web services API by building a plugin and then either publish your API for other developers to build clients or provide your own. Here is a simple example of exposing a method so that a client app can post a status update to the wire in Elgg:

1. Write your function that creates the wire post

function rest_wire_post($username, $text) {
    $user = get_user_by_username($username);
    if (!$user) {
        throw new InvalidParameterException('Bad username');
    }

    $obj = new ElggObject();
    $obj->subtype = 'thewire';
    $obj->owner_guid = $user->guid;
    $obj->access_id = ACCESS_PUBLIC;
    $obj->method = 'api';
    $obj->description = elgg_substr(strip_tags($text), 0, 140);

    $guid = $obj->save();

    add_to_river('river/object/thewire/create',
                 'create',
                 $user->guid,
                 $obj->guid
                );

    return 'success';
}


2. Expose it

expose_function('wire.post',
                'rest_wire_post',
                array( 'username' => array ('type' => 'string'),
                       'text' => array ('type' => 'string'),
                     ),
                'Post a status update to the wire',
                'GET',
                false,
                false);


3. Use it

Type this into your browser's address bar making the obvious replacements for your site:

http://mysite.com/services/api/rest/xml/?method=wire.post&username=cash&text=hello

Now, in a real application you would make this a POST request rather than a GET and protect it with user and API authentication. For more information on that see the Elgg web services wiki page.

1

Recent Comments

Close