PHP Sample Code

Getting the list of companies

<?php
  $api_url = 'https://api.callrail.com/v1/companies.json';
  
  // Replace with your API Key
  $api_key = '';

  $ch = curl_init($api_url);

  curl_setopt($ch, CURLOPT_HEADER, 0);  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Token token=\"{$api_key}\""));

  $json_data = curl_exec($ch);
  $parsed_data = json_decode($json_data);
  curl_close($ch);

  // Examine meta data from the response
  $total_entries = $parsed_data->total_entries;
  echo "Total entries: {$parsed_data->total_entries} \n";

  // Loop through companies
  foreach($parsed_data->companies as $company){
    echo "Company: {$company->name} \n";
  }
?>

Creating a new Company

When creating or updating a resource, remember that a content-type header must be provided.

<?php
  $api_url = 'https://api.callrail.com/v1/companies.json';
    
  // Replace with your API Key
  $api_key = '';

  $ch = curl_init($api_url);
  curl_setopt($ch, CURLOPT_HEADER, 0);  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Token token=\"{$api_key}\"", 
                                             "content-type: application/json"));
  curl_setopt($ch, CURLOPT_POSTFIELDS, '{"name": "WebMarketing Inc", "time_zone": "Pacific Time (US & Canada)"}');
  $json_data = curl_exec($ch);
  $parsed_data = json_decode($json_data);
  curl_close($ch);
  echo $parsed_data;
?>