Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagephp
titleVerify a user is enrolled
linenumberstrue
<?php
// Login to the site and request the access token
$curl = curl_init('http://74your-domain.localhostcom/restws/session/token');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:restws_webservice"); // Your web service user credentials goego here.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$token = curl_exec($curl);
// The token looks like "q71OBx05wtECfjA0KmXf6wiktewrywNhkMZv-OcfyOA%"

// Verify if the user is enrolled in the course
$curl = curl_init("http://74your-domain.localhostcom/course_enrollment.json?uid=200&nid=300");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:restws_webservice"); //Your credentials go here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));

$json = curl_exec($curl);
$response = json_decode($json);
$list = $response->list;

if(empty($list)){
$data = array(
  'nid' => 300,
  'uid' => 200,
  'enrollmenttype' => 'webservice_call',
  'type' => 'webservice_call',
  'status' => 1,
);
// Send the enrollment creation request
$curl = curl_init("http://74.localhost/course_enrollment.json");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:restws_webservice"); //Your credentials go here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"json_encode($data));

$response = curl_exec($curl);
print $response;
}



The response given will be 
$list = $response->list;

A successful creation request will return

Code Block
languagejs
{"uri":"http:\/\/74.localhost\/course_enrollment\/458","id":"458","resource":"course_enrollment"}