How to manage Records using API: PHP sample code

How to manage Records using API: PHP sample code

For those who love to do things the “API way”, we have prepared a sample PHP code, which guides the developer on how to add, update and delete the Records. It is a single-run PHP script doing all three things at once.

The script should be self-explanatory. All you need to do is to update the $apiUser and $apiPass variables, which can be taken from the “Account” -> “Integrations” -> “AutoROICalc API” section.

The script starts with defining the API URLs: to add, update and delete Records. Following is the sample Records array. The first API call is adding the sample Records using the PHP cURL module. The original sample Records array is updated with the ids of the Records that were added and also the Records values are changed. cURL then updates those Records and finally, the sample Records are deleted:

<?php

  // API urls, user and password
  $urlAdd = "https://autoroicalc.com/api/auto-roi-calc/v1/add-records";
  $urlUpdate = "https://autoroicalc.com/api/auto-roi-calc/v1/update-records";
  $urlDelete = "https://autoroicalc.com/api/auto-roi-calc/v1/delete-records";
  $apiUser = "<YOUR_API_USERNAME>";
  $apiPass = "<YOUR_API_PASSWORD>";

  // array of records
  $records = [
    [
      "date" => "2021-04-04",
      "time" => "16:35",
      "type" => "exampleType",
      "activity" => "closed",
      "desc" => "exampleDescription",
      "source" => ["exampleSource1", "exampleSource2"],
      "value" => 13.94
    ],
    [
      "date" => "2021-04-05",
      "time" => "12:09",
      "type" => "exampleType",
      "activity" => "closed",
      "desc" => "exampleDescription",
      "source" => ["exampleSource1"],
      "value" => 20.21
    ]
  ];

  // using cURL
  // ADD RECORDS
  $ch = curl_init($urlAdd);
  $payload = json_encode($records);
  curl_setopt($ch, CURLOPT_USERPWD, "$apiUser:$apiPass");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type:application/json"]);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);

  // print the results from adding the records
  echo "Response from adding the records:\r\n";
  print_r($result);
  echo "\r\n";

  // collect IDs of the added records to demonstrate update
  $response = json_decode($result, true);
  $idsToUpdate = [];
  foreach ($response["results"] as $record) {
    array_push($idsToUpdate, $record["recordId"]);
  }

  // update the original records structure with the retrieved record ids
  // nudge the records value
  $records[0]["id"] = $idsToUpdate[0];
  $records[0]["value"] = 1;
  $records[1]["id"] = $idsToUpdate[1];
  $records[1]["value"] = 2;

  // UPDATE RECORDS
  $ch = curl_init($urlUpdate);
  $payload = json_encode($records);
  curl_setopt($ch, CURLOPT_USERPWD, "$apiUser:$apiPass");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type:application/json"]);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);

  // print the results from updating the records
  echo "Response from updating the records:\r\n";
  print_r($result);
  echo "\r\n";

  // DELETE RECORDS
  $ch = curl_init($urlDelete);
  $payload = json_encode($idsToUpdate);
  curl_setopt($ch, CURLOPT_USERPWD, "$apiUser:$apiPass");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type:application/json"]);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);

  // print the results from updating the records
  echo "Response from deleting the records:\r\n";
  print_r($result);
  echo "\r\n";

?>

Feel free to reach us at support@autoroicalc.com in case of any trouble.


Posted

in

Explore the Records-related tutorials:

Explore the Reports-related tutorials

Explore the Dashboards-related tutorials