Wiki Home Settings page How to use iCafeCloud API

How to use iCafeCloud API

We have added an API feature for iCafeCloud. You generate your API key and secret from Cloud panel “Settings” page API Settings  (Figure 1).

Figure 1

  1. To create your API key and API Secret, click the “Create” button (Figure 2).

Figure 2

  1. It will create the 2 keys “API keys” and “API Secret” (Figure 3).

Figure 3

  1. Click the “Copy” button next to copy the keys (Figure 4).

Figure 4

  1. Paste the keys along with “License ID” to a text file and save it (Figure 5).

Figure 5

Note: Please note that API Secret will only be displayed once when you create it. If you forget to copy it or don't save it. Then, you need to create the keys again to get the API Secret.

  1. Now, download our example file from this link to your server or your web application.
  2. Edit the example file and replace the placeholders with your “License ID”, “API Keys” and “API Secret”  (Figure 6).

Figure 6

  1. Remove any “vardump” in the example file and then you are ready to use the API.
  2. Note the parameters used in the example file (Figure 7)

Figure 7

  1. And create a web form using any frontend framework or just plain HTML/CSS/JS (Figure 8)

Figure 8

Note: The fields with * are required fields to create a member account using the API so those must be included in the form.

  1. When you submit the form,if the account create succeeds the response from the API will be ‘true’ or “”., and if the member already exists then you will receive “Account exists” as response (Figure 9)

Figure 9

  1. Using the response, you can show alerts or perform other actions using javascript (like showing sweet alerts) (Figure 10)

Figure 10

Notes:

  • Current API only supports creating member accounts, other features and functions will be added in future.

/***

 * access icafecloud's api

 * @param $params

 *      array(

            member_account: '',

            member_password: '',

            member_first_name: '',

            member_last_name: '',

            member_birthday: 'yyyy-mm-dd',

            member_expire_time_local: 'yyyy-mm-dd'

            member_email: '',

            member_phone: '',

            member_id_card: '',

            member_balance: 0,

            member_points: 0,

            member_group_name: '',

            member_sex: 'F/M',

            member_comments: '',

            member_address: ''

 *      )

 * @param $method string POST|GET|DELETE|PUT

 * @param $api_key string

 * @param $api_secret string

 * @param $error string  return error message

 * @return bool  true|false

 */

function icafecloud_api_execute($end_point, $params, $method, $license_id, $api_key, $api_secret, &$error)

{

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_VERBOSE, false);

    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $query = http_build_query($params, '', '&');

    $time = time();

    $signature = $license_id . "\x00" . $api_key . "\x00" . $time . "\x00" . strtoupper($method) . "\x00" . $query;

    $signhash = hash_hmac('sha256', $signature, $api_secret);

    $headers = array(

        "X-Time: {$time}",

        "X-License-Id: {$license_id}",

        "X-Auth: {$api_key}:{$signhash}"

    );

    curl_setopt($curl, CURLOPT_URL, 'https://cp.icafecloud.com/api/v1/' . $end_point);

    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($curl, CURLOPT_USERAGENT, "User-Agent: Mozilla/4.0 (compatible; PHP iCafeCloud API)");

    // Post and postfields

    if ($method === "POST") {

        curl_setopt($curl, CURLOPT_POST, true);

        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

    }

    // Delete Method

    if ($method === "DELETE") {

        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

    }

    // PUT Method

    if ($method === "PUT") {

        curl_setopt($curl, CURLOPT_PUT, true);

    }

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($curl, CURLOPT_HEADER, true);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_TIMEOUT, 60);

    $output = curl_exec($curl);

    var_dump($output);

    // Check if any error occurred

    if (curl_errno($curl) > 0) {

        $error = 'Curl error: ' . curl_error($curl);

        return false;

    }

    $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ($http_code != 200) {

        $error = "Curl status is " . $http_code;

        return false;

    }

    $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);

    $output = substr($output, $header_size);

    curl_close($curl);

    $ret_data = json_decode($output, true);

    if ($ret_data === false) {

        $error = 'Invalid response';

        return false;

    }

    $result = $ret_data['result'] ?? 0;

    if ($result == 0) {

        $error = $ret_data['message'] ?? '';

        return false;

    }

    return true;

}

# tests

$params = [

    'member_account' => 'test01',

    'member_password' => '123456',

    'member_first_name' => 'Robert',

    'member_last_name' => 'Wood',

    'member_birthday' => '1999-01-01',

    'member_expire_time_local' => '0000-00-00',

    'member_email' => 'robert.wood.noexists@gmail.com',

    'member_phone' => '13000000001',

    'member_id_card' => 'ID-231302',

    'member_balance' => 0,

    'member_points' => 0,

    'member_group_name' => '',

    'member_sex' => 'M',

    'member_comments' => 'created by api',

    'member_address' => '12621 Frank Loyd'

];

# generate your api key/secret in CP settings

$license_id = '';

$api_key = '';

$api_secret = '';

$ret = icafecloud_api_execute('member', $params, 'POST', $license_id, $api_key, $api_secret, $error);

var_dump($ret);

var_dump($error);

Related: