All course fields, including custom, can be sent via web service, during creation and update calls. The following structure applies to any course type in the system, including sessions, with updates to the appropriate fields. The course type may be updated by changing the type value, seen on line 13.
Internal Value | |
---|---|
Course | course |
Session | group_event_series_event |
NOTE: When creating or updating an entity
An additional token is required for security, and must be passed in an X-CSRF-Token HTTP header. In the example below, the system is queried for this token on lines 2-7, and it is applied to the call on line 28.
For any call, you must specify the format being posted in a Content-Type HTTP header, either "application/json" or "text/xml".
Most content types in Ethos use the body field for the main content, except Courses, which use the field_course_summary field.
Creating a course
<?php $domain = '76.localhost'; $userpass = 'restws_webservice:restws_webservice'; // Login to the site and request the access token $curl = curl_init('http://' . $domain . '/restws/session/token'); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, $userpass); // Your web service user credentials goes here. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); $token = curl_exec($curl); // The token looks like "q71OBx05wtECfjA0KmXf6wiktewrywNhkMZv-OcfyOA%" print_r($token); // Build a course to send, in JSON. The fields and allowed values are available on the full documentation site. $course = array( // A single line textbox 'title' => 'My Course', 'type' => 'course', // A multi-line text area field 'field_course_summary' => array( 'value' => 'A <b>summary</b> of the course', 'format' => 'full_html' ), // It is best practice is to assign the Web Service user id as author 'author' => 1, // Status (published): A single select checkbox. Either 1 or 0 'status' => 1, 'field_course_date' => array( 'value' => '2017-08-20T13:00:00', 'value2' => '2018-08-20T21:00:00', ), 'field_course_event_date' => array( 'value' => '2018-08-20T13:00:00', 'value2' => '2018-08-20T21:00:00', ), 'model' => 'DLC_1234', 'field_learning_objectives' => array( 'value' => '<p>Objectives for learning.</p>', 'format' => 'full_html' ), 'field_hotel_travel' => array( 'value' => '<p>Attendees will have a block of rooms available at <b>The Double Tree</b></p>', 'format' => 'full_html' ), 'field_hotel_information' => array( 'value' => '<p>Please call the front desk for rates.</p>', 'format' => 'full_html' ), "field_hotel_link" => array( "title" => "The Double Tree website", "url" => "doubletree3.hilton.com", ), 'field_venue_phone' => array( 'number' => '2158931600', 'country_code' => 'us' // Link to list ), "field_course_location" => array( "street" => "237 S Broad Street", "additional" => "", "city" => "Philadelphia", "province" => "PA", "postal_code" => "19107", "country" => "us", ), "field_course_live" => 1, // Course Cagetory is field containing a list of taxonomy terms. The id values entered here are the integer term ids "field_course_category" => array( array('id' => 50), array('id' => 60), ), "field_course_format" => array( array('id' => 10), array('id' => 20), ), ); $json = json_encode($course); $curl = curl_init('http://' . $domain . '/node.json'); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, $userpass); //Your credentials go here curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json")); $http_return = curl_exec($curl); $response = json_decode($http_return); print_r($response); // Get the new nid to send credit information $nid = $response->id; // Add Credit $curl = curl_init('http://' . $domain . '/course_credit.json'); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, $userpass); //Your credentials go here curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json")); // Enable Attendance $credit_attendance = array( 'nid' => $nid, 'active' => 1, 'type' => 'attendance', "increments" => '0.00', "min" => '0.00', "max" => '1.00', "enable_variable_credit" => 0, ); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($credit_attendance)); $http_return = curl_exec($curl); $response = json_decode($http_return); print_r($response); // Enable AMA $credit_ama = array( 'nid' => $nid, 'active' => 1, 'type' => 'ama', "increments" => '0.25', "min" => '1.00', "max" => '5.50', "enable_variable_credit" => 1, ); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($credit_ama)); $http_return = curl_exec($curl); $response = json_decode($http_return); print_r($response);
Updating a course's title.
When updating any content, the payload sent is trimmed down to only the fields being updated. To update the course created in the previous section, we use the URI sent, http://your-domain.com/node/33.
<?php $curl = curl_init('http://your-domain.com/restws/session/token'); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, "admin:webservicepw"); //Your credentials go here curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); $token = curl_exec($curl); print "Received token: " . $token . "\n"; // Token looks like "q71OBx05wtECfjA0KmXf6wiktewrywNhkMZv-OcfyOA%" // Build a course // The fields and allowed values are available on the full documentation site. $course = array( 'title' => 'my updated course', ); $json = json_encode($course); $curl = curl_init('http://your-domain.com/node/33'); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, "admin:webservicepw"); //Your credentials go here curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json")); $response = curl_exec($curl); print $response;