Giter Site home page Giter Site logo

Comments (7)

llagerlof avatar llagerlof commented on July 28, 2024 1

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.

winduty avatar winduty commented on July 28, 2024

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.

winduty avatar winduty commented on July 28, 2024

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.

winduty avatar winduty commented on July 28, 2024

there is an example of your code https://github.com/llagerlof/MoodleRest/wiki/MoodleRest-examples

from moodlerest.

llagerlof avatar llagerlof commented on July 28, 2024

@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.

brunocs86 avatar brunocs86 commented on July 28, 2024

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 avatar brunocs86 commented on July 28, 2024

@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.

I will try to carry out your guidelines, thank you very much for helping me.

from moodlerest.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.