Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in file_get_contents #2

Closed
brunocs86 opened this issue Jun 1, 2020 · 7 comments
Closed

Error in file_get_contents #2

brunocs86 opened this issue Jun 1, 2020 · 7 comments
Labels
question Further information is requested

Comments

@brunocs86
Copy link

I have performed a test according to the code below

`<?php

require_once "MoodleRest.php";

$parameters = array('userlist' => array(array('userid' => 13, 'courseid' => 2), array('userid' => 13, 'courseid' => 2)));

$json = (new MoodleRest())->setServerAddress("http://dev.moodle.local/webservice/rest/server.php")->setToken('f0bb33743851adc4c2475240787c07b0')->setReturnFormat(MoodleRest::RETURN_JSON)->request('core_user_get_course_user_profiles', $parameters);

echo $json;`

And I got the following error

image

Can you help me?

@winduty
Copy link

winduty commented Jun 2, 2020

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

@winduty
Copy link

winduty commented Jun 2, 2020

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 */
	
}
?>

@winduty
Copy link

winduty commented Jun 2, 2020

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

@llagerlof
Copy link
Owner

llagerlof commented Jun 3, 2020

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

@llagerlof llagerlof added the question Further information is requested label Jun 3, 2020
@brunocs86
Copy link
Author

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

thanks for your contribution, it was very enlightening

@brunocs86
Copy link
Author

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

@llagerlof
Copy link
Owner

Due to inactivity, and because I am assuming that the author of this issue must have already solved it, I am closing this thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants