Tuesday, March 4, 2014

Mahara web services - simple guide

Mahara web services can be installed as a plugin. Installation is a easy process. you can find the steps in here https://wiki.mahara.org/index.php/Plugins/Auth/WebServices#Installation_Instructions

extract the zip file and put to folders inside mahara /path/to/mahara/webservice and /path/to/mahara/auth/webservice

login to mahara as admin goto menu extensions -> Plugin administration under Plugin type: auth heading install webservice

code check for https so if you do not have SSL in your site and you want to test the web services you can comment out https check part in the code (NOT recommended for a production server)

for example if you want to use REST go to the folder webservice -> rest inside server.php comment

// you must use HTTPS as token based auth is a hazzard without it
/*if (!is_https()) {
    header("HTTP/1.0 403 Forbidden - HTTPS must be used");
    die;
}*/

Now you can test web services using the built in Web service test client
click Configuration link near webservice plugin. Enable WebServices master switch. under Manage Service Access Tokens add a user and generate a token. Now click Web service test client tab and select
protocol - REST
Authentication Type - Token
Service - choose the service you selected when generating token.
Select a function and enter token.

You can also use curl from another site to access the webservice like this

<?php
$service_url = 'http://yoursite.com/mahara/webservice/rest/server.php';
$token = 'your token';

$curl = curl_init($service_url);

$curl_post_data = array(       

 'wsfunction' => 'mahara_user_get_users', // the function to be called
      'wstoken' => $token //token need to be passed in the url
);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);

$info = curl_getinfo($curl);

if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: '.var_export($info));
}
curl_close($curl);

header("Content-type: text/xml");

echo $curl_response;

?>

No comments:

Post a Comment