Web services

 
 
Picture of niranjan reddy
How to create a webservice client in moodle 2.0 using php
 

Hi,

       I created one webservice and assigned the webservice to the user. Can any one tell me how to create the client to access the webservice using php.

With Regards,

Niranjan

 
Average of ratings:Useful (1)
Picture of Alberto del Pozo
Re: How to create a webservice client in moodle 2.0 using php
 
Hi Niranjan.

First of all, sorry for my bad English.

There are four protocols you can use to connect with Moodle (SOAP, XML-RPC, AMF and REST), and I think they're working in a JSON connector. I'll copy here the code to connect via XML-RPC, but if you want to connect using another protocol, just ask here for it. To use the code I'm going to post you will need the Zend framework for php, that you can download from the website (I think you can copy the one that moodle uses, which is in lib/zend.

Then, you will only need this:

<?php

include 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::autoload('Zend_Loader');

$user = "wsuser";
$password = "wspassword";
$function = "moodle_user_get_users_by_id";

$params = array(array(2,3));

$serverurl = "http://127.0.0.1/moodle/webservice/xmlrpc/simpleserver.php";
$serverurl.= "?wsusername=$user";
$serverurl.= "&wspassword=$password";

$client = new Zend_XmlRpc_Client($serverurl);
try {
$data = $client->call($function, $params);
} catch (exception $exception) {
print "<pre>";
var_dump ($exception);
}

print "<pre>";
var_dump ($data);
?>


And that's all.

PD1-> Your site administrator password must be wspassword (you must change the password directly in the database)
PD2->AMF is use for flash, don't know how to connect from php


Cheers.
Pozo.
 
Average of ratings: -
Picture of niranjan reddy
Re: How to create a webservice client in moodle 2.0 using php
 
Hi Pozo,
Thank You.
 
Average of ratings: -
Picture of niranjan reddy
Re: How to create a webservice client in moodle 2.0 using php
 
Hi Pozo,

Thank You for the reply, I did't find the Zend/Loader/Autoloader.php in the moodle lib/zend file and i am using the wampserver with apache 2.2.11 and php 5.3.0 version and moodle 2.0 can u please tell me how to configur you code in my system and i am using the soap protocol


With Regards,
 
Average of ratings: -
Picture of Nagendra Shukla
Re: How to create a webservice client in moodle 2.0 using php
 

Please feel free to provide me your WSDLs, we can create compelete integration libs for client side. or post the wsdl here we will contribute the libs.

Nagendra (nshukla@mtgr8.com)

 
Average of ratings: -
Picture of Rarivonirina Jean
Re: How to create a webservice client in moodle 2.0 using php
 

Hello all.

I'm searching how to implement a client with the REST protocol.

Thank u for helping me, there's 2 week that I'm on it smile

 
Average of ratings: -
Picture of Ibrahim BAKAYOKO
Re: How to create a webservice client in moodle 2.0 using php
 

I am having the same issue.

I have tried this :

<?php

$function = "moodle_user_get_users_by_id";

//$params = array(array(1,2,3));

/*
$params['userids'][] = 2;
$params['userids'][] = 3;
*
*/

$params = array( 'userids' => array(3,4));

$serverurl = "http://{server_ur}/webservice/rest/simpleserver.php?wsusername=wsuser&wspassword=wsuserPassword";


//$serverurl = "http://{server_ur}/webservice/xmlrpc/server.php?wstoken=8dc8f5ca046129706c0d85a56efcaddd";

$postdata = http_build_query($params);

$functionname = '&wsfunction=moodle_user_get_users_by_id';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{$serverurl}.{$functionname}" );
//curl_setopt($ch, CURLOPT_URL, $serverurl );
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
//$data = json_decode(curl_exec($ch));
$data = curl_exec($ch);
curl_close($ch);
print "<pre>";              
var_dump($data);
print "</pre>";

?>

The response i am having is :

 '<?xml version="1.0" encoding="UTF-8" ?>
<EXCEPTION class="webservice_access_exception">
<MESSAGE>Access control exception</MESSAGE>
</EXCEPTION>
'
(length=145)

with the same user i am able to fetch the result via xml-rpc

<?php

include 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::autoload('Zend_Loader');

$function = "moodle_user_get_users_by_id";

$params = array(array(3,4));

//$serverurl = "http://{server_url}/webservice/xmlrpc/simpleserver.php?wsusername=wsuser&wspassword=userPassword";

$serverurl = "http://{server_url}/webservice/xmlrpc/server.php?wstoken=8dc8f5ca046129706c0d85a56efcaddd";

$client = new Zend_XmlRpc_Client($serverurl);
print "<pre>";
try {
$data = $client->call($function, $params);
var_dump ($data);
} catch (exception $exception) {
var_dump ($exception);
}
print "</pre>";

?>

 
Average of ratings: -
Picture of Ibrahim BAKAYOKO
Re: How to create a webservice client in moodle 2.0 using php
 

I got it now.

Rest Client :

<?php

$function = "moodle_user_get_users_by_id";

$serverurl = "http://{server_url}/vclc/webservice/rest/server.php";

$params = array(
'userids' => array(2,4),
'wsfunction' => 'moodle_user_get_users_by_id',
'wstoken' => '157ca217b09caffee3e9719b96698386'
);

$postdata = http_build_query($params);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $serverurl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec($ch);
curl_close($ch);
print "<pre>";
var_dump($data);
print "</pre>";

 
Average of ratings:Useful (1)
Picture of Balaji Kutty
Re: How to create a webservice client in moodle 2.0 using php
 

Hi,

I got this working for getting just the user id enumerated. But, I have problems when I try to get courses. I started a separate discussion on this. But, would like to post it here too. Please point me what is wrong in my setup.

This is on Moodle 2.0 on Ubuntu GNU/Linux with Apache 2.0 and MySQL.

I created a user for web authentication and created a role called webaccess. This user is assigned to this role. I assigned all access to this role. I also created external services for xmlrpc with all functions added to it. I'm able to see the users using the function moodle_user_get_users_by_id. But, when I try to access the courses, I get the following error.

"error/You cannot execute functions in the course context (course id:2). The context error message was: Course or activity not accessible."

I using the following XML RPC code

<?php

include 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::autoload('Zend_Loader');

$function = "moodle_course_get_courses";

$params = array('options' => array('ids' => array(2)));

$serverurl = "http://localhost/moodle/webservice/xmlrpc/server.php?wstoken=4647d51e7a4cc7f97913658d044370a9";

$client = new Zend_XmlRpc_Client($serverurl);
echo "Testing xml rpc";
print "<pre>";
try {
    $data = $client->call($function, $params);
    var_dump ($data);
    echo $data;
} catch (exception $exception) {
    var_dump ($exception);
}
print "</pre>";
?>


I could get the course listed only after commenting out the context checking code in course/externallib.php like shown below.

/*            $context = get_context_instance(CONTEXT_COURSE, $course->id);
            try {
                self::validate_context($context);
            } catch (Exception $e) {
                $exceptionparam = new stdClass();
                $exceptionparam->message = $e->getMessage();
                $exceptionparam->courseid = $course->id;
                throw new moodle_exception(
                        get_string('errorcoursecontextnotvalid', 'webservice', $exceptionparam));
            }*/

I'm unable to get the context set for webservices. What should I do to have a seemless access to courses through webservice using xmlrpc?

 

Thanks

Balaji

 
Average of ratings: -
Picture of Philippe Siwinski
Re: How to create a webservice client in moodle 2.0 using php
 

Just for other Moodlers, the "course context" error occurs when the web service user does not have the permission to View courses without participation (moodle/course:view).

Allow it and everything is gonna be alright.

 
Average of ratings: -
Picture of Josh Jones
Re: How to create a webservice client in moodle 2.0 using php
 

Hi guys, 

I successfully manage to call get functions using rest, such as get_user, get_course ect, however I can't seem to create courses, or users. 

<?php

include 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::autoload('Zend_Loader');
$params = array(
'wsfunction' => 'moodle_enrol_manual_enrol_users',
'wstoken' => 'werw90483590438509sdwejhfjdkwfh3294',
'roleid' => '1',
'userid' => '3',
'courseid' => '2',
);

$serverurl = "http://localhost/moodle/webservice/rest/server.php";

$client = new Zend_Http_Client($serverurl);

print "<pre>";

$client->setParameterPost($params);
$response = $client->request('POST');
echo ($response->getbody());

print "</pre>";

?>
 
This is my code for enrol a user, I get the error invalid parameter. Does anyone know the correct parameters for this and for creating users?
Thank you all. 
 
Average of ratings: -
Picture of albert trabal arroyo
Re: How to create a webservice client in moodle 2.0 using php
 

Hi,

I need this for Framework php CodeIgniter. Is possible?

Thanks

 
Average of ratings: -
Picture of roc mehra
Re: How to create a webservice client in moodle 2.0 using php
 

Hi,

I have never used zend. How can i call my webservice?

 
Average of ratings: -
Picture of Rene P
Re: How to create a webservice client in moodle 2.0 using php
 

You mean in Zend Framework? Have you got answer for your question?

 
Average of ratings: -