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
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)
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
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>";
?>
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>";
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
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.
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.
Hi,
I need this for Framework php CodeIgniter. Is possible?
Thanks
Hi,
I have never used zend. How can i call my webservice?
You mean in Zend Framework? Have you got answer for your question?