llagerlof / MoodleRest

MoodleRest is a PHP class to query Moodle REST webservices
MIT License
86 stars 21 forks source link

Error in file_get_contents #2

Closed brunocs86 closed 3 years ago

brunocs86 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

llagerlof commented 4 years ago

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

brunocs86 commented 4 years ago

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

thanks for your contribution, it was very enlightening

brunocs86 commented 4 years ago

@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 commented 3 years ago

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