PHP SDK for Moodle RESTful APIs
-
Install MoodleSDK
php composer.phar require agurz/moodle-php-sdk
-
Create a
RestApiContext
instance$context = new RestApiContext(); $context->setUrl('example.com/moodle') ->setCredential(new AuthTokenCredential('token'))
-
Create a model object instance, set it's properties and call
get
,create
,update
, ordelete
operations$user = new User(); $user->setUsername('username') ->setPassword('Password..01') ->setFirstName('first') ->setLastName('last') ->setFullName('first last') ->setEmail('[email protected]') ->create($context)
-
That's all!
<?php
require_once 'vendor/autoload.php';
use MoodleSDK\Api\Model\Course;
use MoodleSDK\Api\Model\User;
use MoodleSDK\Auth\AuthTokenCredential;
use MoodleSDK\Rest\RestApiContext;
$context = RestApiContext::instance()
->setUrl('example.com/moodle')
->setCredential(new AuthTokenCredential('token'))
$user = User::instance()
->setUsername('agurz')
->setPassword('Password..01')
->setFirstName('Agustn')
->setLastName('Rodríguez')
->setFullName('Agustn Rodríguez')
->setEmail('[email protected]')
->create($context);
$course = Course::instance()
->setShortName('test-course')
->get($context)
->enrolUser($context, $user);