How to create a webservice client in moodle 2.0 using php

How to create a webservice client in moodle 2.0 using php

by niranjan reddy -
Number of replies: 13

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)
In reply to niranjan reddy

Re: How to create a webservice client in moodle 2.0 using php

by Alberto del Pozo -
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.
In reply to Alberto del Pozo

Re: How to create a webservice client in moodle 2.0 using php

by niranjan reddy -
Hi Pozo,
Thank You.
In reply to niranjan reddy

Re: How to create a webservice client in moodle 2.0 using php

by niranjan reddy -
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,
In reply to niranjan reddy

Re: How to create a webservice client in moodle 2.0 using php

by Nagendra Shukla -

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)

In reply to Alberto del Pozo

Re: How to create a webservice client in moodle 2.0 using php

by Rarivonirina Jean -

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

In reply to Rarivonirina Jean

Re: How to create a webservice client in moodle 2.0 using php

by Ibrahim BAKAYOKO -

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>";

?>

In reply to Ibrahim BAKAYOKO

Re: How to create a webservice client in moodle 2.0 using php

by Ibrahim BAKAYOKO -

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)
In reply to Ibrahim BAKAYOKO

Re: How to create a webservice client in moodle 2.0 using php

by Balaji Kutty -

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

In reply to Alberto del Pozo

Re: How to create a webservice client in moodle 2.0 using php

by Josh Jones -

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. 
In reply to Alberto del Pozo

Re: How to create a webservice client in moodle 2.0 using php

by albert trabal arroyo -

Hi,

I need this for Framework php CodeIgniter. Is possible?

Thanks

In reply to Alberto del Pozo

Re: How to create a webservice client in moodle 2.0 using php

by roc mehra -

Hi,

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

In reply to roc mehra

Re: How to create a webservice client in moodle 2.0 using php

by Rene P -

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