Comments (7)
Due to inactivity, and because I am assuming that the author of this issue must have already solved it, I am closing this thread.
from moodlerest.
I recommend a tool to test the web services called Postman, with it you can know how to test the functions and determine if you are having any error in the handling of the functions, a problem on the server or a configuration problem of the web service
from moodlerest.
Personally, I developed this file to handle the functions that I have enabled in my web service to call them from any php file where I am going to use them.
require_once "MoodleRest.php";
class Moodle
{
private $Moodle;
private $token;
function __construct()
{
$this->Moodle = new MoodleRest();
$this->token = $this->getToken();
}
//get web service token
function getToken()
{
$url = 'https://url of moodle site/login/token.php?';
$data = array(
'username' => 'webservice_user_name',
'service' => 'webservicename',
'password' => 'webservice_user_password'
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$array = json_decode($result, true);
return $array['token'];
}
//conenction to the web service using the token
function connection()
{
$ip = 'url_of_moodle_site';
$this->Moodle->setServerAddress("https://$ip/webservice/rest/server.php");
$this->Moodle->setToken($this->token);
return $this->Moodle->setReturnFormat(MoodleRest::RETURN_ARRAY);
}
/* any enabled function on web service for example core_user_get_users */
function get_course_profiles($userid,$courseid)
{
if($this->connection()):
$info = array
(
'userid' => $userid,
'courseid' => $courseid
);
$params = array( 'userlist' => array($info));
$ARRAY = $this->Moodle->request('core_user_get_course_user_profiles', $params);
return "result2:\n" . print_r($ARRAY, true);
else:
return 0; //or wherever you want
endif;
}
function GetUserId($username)
{
if($this->connection()):
$username = array
(
'key' => 'username' ,
'value' => $username
);
$params = array( 'criteria' => array($username));
$ARRAY = $this->Moodle->request('core_user_get_users', $params);
return $ARRAY['users'][0]['id'];
die();
else:
echo "Error de conexion";
endif;
}
function UserPasswordReset($username,$password)
{
if($this->connection()):
$info = array
(
'id' => $this->GetUserId($username),
'password' => $password,
'preferences' => array($preferences = array
(
'type' =>'auth_forcepasswordchange',
'value' => 0
))
);
$params = array( 'users' => array($info));
$ARRAY = $this->Moodle->request('core_user_update_users', $params);
//print_r( $this->Moodle->getPrintOnRequest());
return 1;
else:
return 0;
endif;
}
/* end of function management */
}
?>
from moodlerest.
there is an example of your code https://github.com/llagerlof/MoodleRest/wiki/MoodleRest-examples
from moodlerest.
@brunocs86 Hi Bruno.
I tested your code here, making the necessary changes to my environment and it worked.
Looks like you have a name resolution problem. This can be caused for some reasons.
Please, do this tests:
Try to wget the server.
$ wget http://dev.moodle.local/webservice/rest/server.php -O wget.txt
Open the wget.txt
. Should be something like this:
<?xml version="1.0" encoding="UTF-8" ?>
<EXCEPTION class="moodle_exception">
<ERRORCODE>invalidtoken</ERRORCODE>
<MESSAGE>Invalid token - token not found</MESSAGE>
</EXCEPTION>
a. If wget cannot find the host, wget.txt
will be empty and you will receive the error wget: unable to resolve host address
. In this case the problem it's in your network.
b. If wget find your server, it could be doing some redirection. In this case try using the IP of your Moodle server instead the host name. In setServerAddress()
replace the host dev.moodle.local
by the IP of your moodle and make the request.
c. If everything fails, just to cover most scenarios, try to wget using the IP instead the hostname.
Please, report the results here so we can find a solution for your case.
from moodlerest.
there is an example of your code https://github.com/llagerlof/MoodleRest/wiki/MoodleRest-examples
thanks for your contribution, it was very enlightening
from moodlerest.
@brunocs86 Hi Bruno.
I tested your code here, making the necessary changes to my environment and it worked.
Looks like you have a name resolution problem. This can be caused for some reasons.
Please, do this tests:
Try to wget the server.
$ wget http://dev.moodle.local/webservice/rest/server.php -O wget.txt
Open the
wget.txt
. Should be something like this:<?xml version="1.0" encoding="UTF-8" ?> <EXCEPTION class="moodle_exception"> <ERRORCODE>invalidtoken</ERRORCODE> <MESSAGE>Invalid token - token not found</MESSAGE> </EXCEPTION>
a. If wget cannot find the host,
wget.txt
will be empty and you will receive the errorwget: unable to resolve host address
. In this case the problem it's in your network.b. If wget find your server, it could be doing some redirection. In this case try using the IP of your Moodle server instead the host name. In
setServerAddress()
replace the hostdev.moodle.local
by the IP of your moodle and make the request.c. If everything fails, just to cover most scenarios, try to wget using the IP instead the hostname.
Please, report the results here so we can find a solution for your case.
I will try to carry out your guidelines, thank you very much for helping me.
from moodlerest.
Related Issues (6)
- add composer support HOT 2
- invalid argument error HOT 2
- Request to call before core_user_get_course_user_profiles
- Add a namespace so that the class can be easily autoloaded by frameworks like Laravel HOT 1
- file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moodlerest.