Versions Compared

Key

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

...

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

// Send the query request via GET
$curl = curl_init("http://your-domain.com/uc_cart_item.json?cart_id=200");
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"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

$json = curl_exec($curl);
$response = json_decode($json);
$product_list = $response['list'];

...

Code Block
languagephp
titleEnrolling the user
linenumberstrue
// Iterate through the products to create the enrollment request
foreach ($product_list as $product) {
  $data = array(
    'nid' => $product->nid,
    'uid' => 200,
    'enrollmenttype' => 'webservice_call', // enrollmenttype and type should be updated per client system. They are unique identifiers of the external system sending the call. 
    'type' => 'webservice_call', // Formatted in lower case alphanumeric and underscore characters. The two values are not required to be identical.
    'status' => 1,
  );
// Send the enrollment creation request
  $curl = curl_init("http://your-domain.com/course_enrollment");
  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"); //Your credentials go here
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

  $response = curl_exec($curl);
} 

...

Code Block
languagephp
titleDeleting cart items
linenumberstrue
// Iterate through the products to create the deletion request
foreach ($product_list as $product) {
  $curl = curl_init("http://your-domain.com/uc_cart_item/{$product->nid}");
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
  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"); //Your credentials goes here
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

  $json = curl_exec($curl);
  $cinfo = curl_getinfo($curl);
  $response = json_decode($json);
  if ($cinfo['http_code'] == 200 && empty($response)) {
    // The entity has been deleted.
  }
  else {
    // An error has occurred.
  }
}

...