Versions Compared

Key

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

...

Code Block
languagephp
<?php

$domain = '75.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_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"$userpass); // Your web service user credentials goes here.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$token = curl_exec($curl);
print_r("TOKEN: $token \n");
// The token looks like "q71OBx05wtECfjA0KmXf6wiktewrywNhkMZv-OcfyOA%"
// #1 Verify the user does not exist by sending a GET request to the system, searching by user 'mail' attribute
$curl = curl_init("http://$domain/user.json?mail=newuser@dlc-solutions.com");
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:webservice_password"$userpass); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$json = curl_exec($curl);
$response = json_decode($json);
print "\nUser search ";

// A list of users matching criteria will be returned. If this list is empty, a user does not exist in EthosCE with the given e-mail address.
$list = $response->list;

// #2 Send the user creation request, via POST
if (empty($list)) {
  $user = array(
    'name' => 'newuser_username', // Username
    'mail' => 'newuser@dlc-solutionsnewuser@ethosce.com',
    'status' => 1, // Enabled. A value of 0 sets the account as 'Blocked'
  );
  $curl = curl_init("http://$domain/user");
  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:webservice_password"$userpass); //Your credentials goes here
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user));

  $json = curl_exec($curl);
  print_r("$json \n");
  // An array detailing the new user entity is returned if successful
  $userInfo = json_decode($json);

  print "Created\nCreated user $userInfo->id http://$domain/user/$userInfo->id";

  // Prepare the profile creation calls
  $curl = curl_init("http://$domain/profile2");
  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:webservice_password"$userpass); //Your credentials goes here
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

  // User profile data
  $profile = array(
    'user' => $userInfo->id, // The value sent back is a user entity, which identifies itself by entity id, which is also the Drupal uid
    'label' => 'Profile',
    'type' => 'profile',
    'field_first_name' => 'John',
    'field_middle_name' => 'Middle',
    'field_last_name' => 'Smith',
    'field_profile_location' => array(
      'street' => '1520 Locust Street',
      'additional' => 'Suite 1000',
      'city' => 'Philadelphia',
      'province' => 'PA',
      'postal_code' => '19102',
      'country' => 'us', // A two character country code
    ),
  );

  // Create the main profile
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($profile));
  $json = curl_exec($curl);
  print_r("$json \n");
  $response = json_decode($json);
  print "Created\nCreated profile $response->id";

// User Bio data
  $bio = array(
    'user' => $userInfo->id,
    'label' => 'Bio',
    'type' => 'bio',
    'field_fm_biogrpahybiography' => array(
      'value' => 'This is the learner <b>bio</b>",',
      'format' => 'filtered_html', // Text format is required. Filtered HTML is availabe to all learners
    ),
    'field_fm_photo' => array(
      'entity_type' => 'file',
      'id' => '207',
    ),
    'field_credentials' => 'PhD, MD',
  );

  // Create the bio
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bio));
  $json = curl_exec($curl);
  $response = json_decode($json);
  print "Created\nCreated bio $response->id. ";

  // User disclosure data
  $disclosure = array(
    'user' => $userInfo->id,
    'label' => 'Disclosure',
    'type' => 'disclosure',
    'field_fm_disclose' => 1,
  );

  // Create the disclosure
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($disclosure));
  $json = curl_exec($curl);
  $disclosure_new = json_decode($json);
  print "Created disc $response->id";

  // Add financial relationships field collections to the disclosure
  $financial_info = array(
    array(
      'field_fm_attribution' => 'Other',
      'field_fm_commercial_interest' => 'Mayo Clinic',
      'field_fm_dates' =>
      array(
        'value' => 1408492800,
        'value2' => 1409184000,
      ),
      'field_fm_relationship_type' => 'Other',
      'field_fm_relationship_type_other' => 'I bought supplies which were reimbursed.',
      'field_name' => 'field_fm_financial_relationships',
      'host_entity' => array(
        id => $disclosure_new->id,
        resource => "profile2",
      ),
    ),
    array(
      'field_fm_attribution' => 'Self',
      'field_fm_commercial_interest' => 'Mayo Clinic',
      'field_fm_dates' =>
      array(
        'value' => 1440028800,
        'value2' => 1440720000,
      ),
      'field_fm_relationship_type' => 'Employment',
      'field_fm_relationship_type_other' => 'Paid sponsorship of venue.',
      'field_name' => 'field_fm_financial_relationships',
      'host_entity' => array(
        id => $disclosure_new->id,
        resource => "profile2",
      ),
    ),
  );

  foreach ($financial_info as $item) {
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($item));
    $json = curl_exec($curl);
    $response = json_decode($json);
  }\nCreated disc $disclosure_new->id. ";
}