# PHP Library

### Install and Create an Application <a href="#install" id="install"></a>

#### **A. Requirements**

* Already installed the sublime application, if you don't have the sublime application please download it at the following link. [Follow this tutorial](https://www.sublimetext.com/download).
* Already installed the xampp application, if you don't have the xampp application please download it at the following link. [Follow this tutorial](https://www.apachefriends.org/download.html).<br>

#### **B. Steps**

1. Please download the antares php library and save it on your local computer by clicking the button below.

{% file src="<https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/aXXk4vjfGXbGr4uMPX5W/antares-php-master.zip>" %}

2. Firstly, register to create an antares account by [clicking register now](https://www.sublimetext.com/download).<br>

   <figure><img src="https://antares.id/assets/img/baru11.png" alt=""><figcaption></figcaption></figure>
3. Next, fill in the email, password and name on Antares and click the create account button and wait for the notification.

   <figure><img src="https://antares.id/assets/img/baru13.png" alt=""><figcaption></figcaption></figure>
4. Next, open the email to verify your Antares account.<br>

   <figure><img src="https://antares.id/assets/img/baru14.png" alt=""><figcaption></figcaption></figure>
5. After verification is complete, login to the Antares account that you have created<br>

   <figure><img src="https://antares.id/assets/img/baru3.png" alt=""><figcaption></figcaption></figure>
6. After logging into the Antares account, the next step is to click account, an example is shown in the image below<br>

   <figure><img src="https://antares.id/assets/img/baru4.png" alt=""><figcaption></figcaption></figure>
7. After clicking the account, the Access Key will appear and then click save access key to proceed to the next step.<br>

   <figure><img src="https://antares.id/assets/img/baru5.png" alt=""><figcaption></figcaption></figure>
8. Create application, if you have already created an application, ignore it and continue to the next process.

   <figure><img src="https://antares.id/assets/img/baru22.png" alt=""><figcaption></figcaption></figure>
9. Next, click add to create an application on the Antares platform.<br>

   <figure><img src="https://antares.id/assets/img/baru23.png" alt=""><figcaption></figcaption></figure>
10. Create device, if you have already created the device you can ignore this process and proceed to the next step.

    <figure><img src="https://antares.id/assets/img/baru30.png" alt=""><figcaption></figcaption></figure>
11. First create a device name as shown below, in this tutorial the device will be named **"sensorSignal"**. then also **select one of the active packages that will be used for the device to be created**. In this tutorial using a trial package where the package is only active for seven days. If you have set the device name and selected the package then click +AddFirst create a device name as shown below, in this tutorial the device will be named "sensorSignal". then also select one of the active packages that will be used for the device to be created. In this tutorial using a trial package where the package is only active for seven days. If you have set the device name and selected the package then click **+Add**

<figure><img src="https://3873791589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cujmJ5QHdJaAjH815aZ%2Fuploads%2Ft1Ye5qMZW14XvoA7tMRt%2Fimage.png?alt=media&#x26;token=7535b766-2bdb-4ec6-a112-729c4c293c48" alt=""><figcaption></figcaption></figure>

A display of the device that has been created will appear. Data from the device will be entered and displayed in the table provided as shown below.

<figure><img src="https://3873791589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cujmJ5QHdJaAjH815aZ%2Fuploads%2FiOw1k6f0ySll3j3r0bZB%2Fimage.png?alt=media&#x26;token=6f05ff28-a443-42d1-881a-ed689cebc139" alt=""><figcaption></figcaption></figure>

### **Start Apache on XAMPP**

1. Next extract the php library that has been downloaded and then copy it to C: \xampp\htdocs, in this tutorial xampp is installed in the C directory.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/Tlv0roBiBfNdWBUxGobv/image.png" alt=""><figcaption></figcaption></figure>

2. Next, open xampp then turn on the apache module and wait until the module is green, for example the image as below.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/RZVx9460x0Srgyx1EHk9/image.png" alt=""><figcaption></figcaption></figure>

### **Get all Devices on Application with Latest Data**

1. Open the sublime/visualcode/notepad++ application then drag the example.php and Antares.php that have been copied to the xampp htdocs.
2. Next, use the script section in example.php and also fill in the variables according to the example below. For other lines, you can comment firs

<pre class="language-php" data-line-numbers><code class="lang-php">require_once('./Antares.php');

Antares::init([
  "PLATFORM_URL" => 'https://platform.antares.id:8443', // TODO: Change this to your platform URL
  "ACCESS_KEY" => '{{accesskey}}' // TODO: Change this to your access key
]);
try {
  // RETRIEVE DATA
  echo "============================ Retrieve data =================================\n";
  echo "";
  // get application
  // application name example '/antares-cse/antares-id/example'
  $resp = Antares::getInstance()->get('/antares-cse/antares-id/{{applicationname}}'); // TODO: Change this to your application uri
  if ($resp instanceof AE) {
    echo nl2br("AE: " . $resp->getName() . "\n");
    
    // list all application's devices
    $cntUris = $resp->listContainerUris();
    echo "Containers: " . count($cntUris) . "\n";
    echo "";
    
    foreach ($cntUris as $cntUri) {
      echo "  " . $cntUri . "\n";
      echo "";

      // get device
      $cnt = Antares::getInstance()->get($cntUri);
      echo "    " . $cnt->getName() . "\n";
      echo "";

      try {
        // get latest data
        $la = $cnt->getLatestContentInstace();
        echo "      [$la->ct]:$la->rn $la->con\n";
        echo "";
      } catch (Exception $e) {
        echo "      last data: " . $e->getMessage() . "\n";
        echo "";
      }
<strong>    }
</strong>  }
} catch (Exception $e) {
  echo($e->getMessage());
}

</code></pre>

3. The next step is to open <http://localhost/antares-php/example.php> (according to the storage folder in htdocs) in the browser, then the following results will appear:

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/ibRQhVsqC9paqDT9krGx/image.png" alt=""><figcaption></figcaption></figure>

### **Get Data ID on Devices by Limit**

1. Open the sublime/visualcode/notepad++ application then drag the example.php and Antares.php that have been copied to the xampp htdocs.
2. Next, use the script section in example.php and also fill in the variables according to the example below. For other lines, you can comment first

   <pre class="language-php" data-line-numbers><code class="lang-php">require_once('./Antares.php');

   Antares::init([
     "PLATFORM_URL" => 'https://platform.antares.id:8443', // TODO: Change this to your platform URL
     "ACCESS_KEY" => '{{accesskey}}' // TODO: Change this to your access key
   ]);
   try {
     // DISCOVERY LIMIT AND OFFSET
     echo "\n\n";
     echo "";
     echo "============================ Discovery =================================\n";
     echo "";
     // application name &#x26; device name example '/antares-cse/antares-id/example/example'
     $cnt = Antares::getInstance()->get('/antares-cse/antares-id/{{applicationame}}/{{devicename}}'); // TODO: Change this to your container uri
     $first10 = $cnt->listContentInstanceUris(10);
     
     // print first10
     foreach ($first10 as $uri) {
       echo $uri . "\n";
       echo "";
     }
     echo "==============================\n";
     echo "";
     $next10 = $cnt->listContentInstanceUris(10, count($first10));
     // print next10
     foreach ($next10 as $uri) {
       echo $uri . "\n";
       echo "";
     }
     
     $inc = 1;
     echo "==============================\n";
     echo "";
     $next10 = $cnt->listContentInstanceUris(10, count($first10) * $inc++ + count($next10));
     // print next10
     foreach ($next10 as $uri) {
       echo $uri . "\n";
       echo "";
     }

     echo "==============================\n";
     echo "";
     $next10 = $cnt->listContentInstanceUris(10, count($first10) * $inc++ + count($next10));
     // print next10
     foreach ($next10 as $uri) {
       echo $uri . "\n";
       echo "";
     }
     
     
     echo "==============================\n";
     echo "";
     $next10 = $cnt->listContentInstanceUris(10, count($first10) * $inc++ + count($next10));
     // print next10
     foreach ($next10 as $uri) {
       echo $uri . "\n";
       echo "";
     }
   } catch (Exception $e) {
     echo($e->getMessage());
   }
   </code></pre>
3. The next step is to open <http://localhost/antares-php/example.php> (according to the storage folder in htdocs) in the browser, then the following results will appear:<br>

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/z1dq6jsO8TJVFKyTTcOk/image.png" alt=""><figcaption></figcaption></figure>

### **Store Data to Devices**

1. Open the sublime/visualcode/notepad++ application then drag the example.php and Antares.php that have been copied to the xampp htdocs.
2. Next, use the script section in example.php and also fill in the variables according to the example below. For other lines, you can comment first

   <pre class="language-php" data-line-numbers><code class="lang-php">require_once('./Antares.php');

   Antares::init([
     "PLATFORM_URL" => 'https://platform.antares.id:8443', // TODO: Change this to your platform URL
     "ACCESS_KEY" => '{{accesskey}}' // TODO: Change this to your access key
   ]);
   try {
     // STORE DATA
     echo "\n\n";
     echo "============================ Insert data =================================\n";
     echo "";
     try {
       // application name &#x26; device name example '/antares-cse/antares-id/example/example'
       $cnt = Antares::getInstance()->get('/antares-cse/antares-id/{{applicationame}}/{{devicename}}'); // TODO: Change this to your container uri
       $lastCin = $cnt->getLatestContentInstace();
       echo "Last CIN: [$lastCin->ct]:$lastCin->rn $lastCin->con\n";
       echo "";
     } catch (Exception $e) {
       echo "Last CIN: " . $e->getMessage() . "\n";
       echo "";
     }
     try {
       $cnt->insertContentInstance('{"help":2.0}', 'application/json');
       $lastCin = $cnt->getLatestContentInstace();
       echo "Current Last CIN: [$lastCin->ct]:$lastCin->rn $lastCin->con\n";
       echo "";
     } catch (Exception $e) {
       echo "Inserting CIN: " . $e->getMessage() . "\n";
       echo "";
     }
   } catch (Exception $e) {
     echo($e->getMessage());
   }
   </code></pre>
3. The next step is to open <http://localhost/antares-php/example.php> (according to the storage folder in htdocs) in the browser, then the following results will appear:

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/EO0zBwxO5ApXVjl9X50s/image.png" alt=""><figcaption></figcaption></figure>
