PUT Vehicle - PHP

The PUT Vehicle method within the Customer Meta Data – Vehicles API enables integrated users to edit the vehicle object details within the referenced REVEAL account.

Before attempting to retrieve vehicle location information it is important to know if the REVEAL account has been updated with valid VEHICLE NUMBERS for each vehicle to be pulled via the API. The VEHICLE NUMBER and VEHICLE NAME fields are different fields within REVEAL and the VEHICLE NUMBER field is not automatically populated upon account creation.

<?php
   $token = 'TOKEN_STRING';
   $FIM_app_id = 'APP_ID';
   $FIM_endpoint = 'https://fim.api.ENV.fleetmatics.com/cmd/v1/vehicles/';

   //Set local variables
   $vehicle_number = 'VEHICLE_NUMBER';
   $vehicle_reg_number = '12345HAQS';
   $vehicle_vin = '12FG4561523A415F153';
   $vehicle_make = 'FORD';
   $vehicle_year = '2012';
   $vehicle_model = 'Fiesta';
   $vehicle_tankCapacity = 20.5;
   $vehicle_highwayMPG = 14.5;
   $vehicle_cityMPG = 11.5;
   $vehicle_size = 1;
   
   //Using variables, create an array to store data for PUT. Note: '=>' is necessary
   $put_data = array('RegistrationNumber' => $vehicle_reg_number,
                     'VIN' => $vehicle_vin,
                     'Make' => $vehicle_make,
                     'Year' => $vehicle_year,
                     'Model' => $vehicle_model,
                     'TankCapacity' => $vehicle_tankCapacity,
                     'HighwayMPG' => $vehicle_highwayMPG,
                     'CityMPG' => $vehicle_cityMPG,
                     'VehicleSize' => $vehicle_size					 
                     );

   //Call method
   echo put_vehicle_info($vehicle_number, $put_data, $FIM_endpoint, $FIM_app_id, $token);

   function put_vehicle_info($vehicle_number, $data, $endpoint, $app_id, $token)
   {
       //Append vehicle number onto the end of the endpoint string for a complete url
       $url = $endpoint.$vehicle_number;  
   
       //Get necessary headers for REST call
       $headers = get_call_headers($app_id, $token);
       
       //convert data to JSON format
       $json_data = json_encode($data);
       
       $session = curl_init($url);                                 //Initialize transfer with URL
       curl_setopt($session, CURLOPT_CUSTOMREQUEST, "PUT");        //Set to PUT transfer request type
       curl_setopt($session, CURLOPT_RETURNTRANSFER, true);        //Return transfer as a string of the return value of curl_exec() 
       curl_setopt($session, CURLOPT_HTTPHEADER, $headers);        //Pass in headers 
       curl_setopt($session, CURLOPT_POSTFIELDS, $json_data);      //Pass in PUT data 
   
       //Execute transfer of $session    
       $response = curl_exec($session); 
       
       //Get http code outcome of the #session transfer    
       $http_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
       
       //Measure false response/error
       if($response === false)
       {
           echo 'Error: '. curl_error($session);
       }
   
       //ALWAYS close transfer connection 
       curl_close($session);
        
       //Evaluate variable for non 204(No Content) http code
       if($http_code !== 204)
       {
           echo 'Error: Http Status Code returned '.$http_code;
       }

       return $response;    
   }
   
   function get_call_headers($app_id, $token)
   {
       //Inserts app_id and token into respective '%s' spaces in the auth header   
       $auth_header = sprintf('Authorization: Atmosphere atmosphere_app_id=%s, Bearer %s', $app_id, $token);
      
       //Create necessary headers for REST call   
       $headers = array();
       $headers[] = $auth_header;  
       //IMPORTANT! - You must use 'Content-Type' as opposed to Accept as you are describing how the info is being sent
       $headers[] = 'Content-Type: application/json'; 
       
       return $headers;
   }
?>

To download source code for this example, click HERE.

DISCLAIMER

All sample code is provided by Fleetmatics, A Verizon Company, for illustrative purposes only. These examples have not been thoroughly tested under all conditions. Fleetmatics, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. All programs contained herein are provided to you “AS IS” without any warranties of any kind.