Moodle API Integration not working. What could be the issue?

122 Views Asked by At

I'm doing an integration between a laravel + API of Moodle regarding the manual enrol of a user to a specific course.

Before this, I did the process of create the user using the API, and it works like a chairman.

This is my code:

$client = new Client();
    $moodleUrl = config('services.moodle.moodle_url');
    $username = config('services.moodle.moodle_user');
    $password = config('services.moodle.moodle_password');
    $courseId = 1;
    $userId = 1;

    $response = $client->post("$moodleUrl/webservice/rest/server.php", [
        'form_params' => [
            'wstoken' => config('services.moodle.moodle_token_id'),
            'wsfunction' => 'enrol_manual_unenrol_users',
            'moodlewsrestformat' => 'json',
            'enrolments[0][roleid]' => 5,
            'enrolments[0][userid]' => $userId,
            'enrolments[0][courseid]' => $courseId
        ],
        'auth' => [$username, $password],
    ]);
    if ($response->getStatusCode() === 200) {
        return true;
    } else {
        return false;
    }

So, when I execute this it gives me: "null", however it doesn't give access to the user to the course.

My moodle application with their API Web Service has the correct access regarding the enrol_manual_unenrol_users, however... is not working as I explained above.

I did a test using postman, and is not working.

Does anyone know what could be the issue?

Kind Regards

0

There are 0 best solutions below