$TESLA_CLIENT_ID="-----------";
$TESLA_CLIENT_SECRET="#################";
$TESLA_API_URL = "https://owner-api.teslamotors.com/";
$API_LAST_ERROR="";
//get vehicle list
function commandListCars($token) {
global $TESLA_API_URL,$API_LAST_ERROR;
echo queryTeslaAPI($token, "api/1/vehicles/");
}
function commandUnlock($token, $VEHICLE_ID) {
global $TESLA_API_URL,$API_LAST_ERROR;
queryTeslaAPI($token, "api/1/vehicles/".$VEHICLE_ID."/command/door_unlock");
}
function commandLock($token, $VEHICLE_ID) {
global $TESLA_API_URL,$API_LAST_ERROR;
queryTeslaAPI($token, "api/1/vehicles/".$VEHICLE_ID."/command/door_lock");
}
//general function to query the tesla API
function queryTeslaAPI ($token, $url) {
global $TESLA_API_URL,$API_LAST_ERROR;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $TESLA_API_URL.$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $token"
));
$response = curl_exec($ch);
curl_close($ch);
$response_array = json_decode ($response, true);
$API_LAST_ERROR=$response_array;
return $response_array["response"];
}
function ReadTeslaAPI($token,$carid) {
global $TESLA_API_URL,$API_LAST_ERROR;
$climate= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/climate_state");
$drive= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/drive_state");
$charge= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/charge_state");
$state= queryTeslaAPI($token, "api/1/vehicles/".$carid."/data_request/vehicle_state");
$api=array();
$api["rated"]=(int)$charge["battery_range"];
$api["soc"]=(int)$charge["battery_level"];
$api["odometer"]=(int)$state["odometer"];
$api["ctemp"]=$climate["outside_temp"] ;
$api["temp"]=($climate["outside_temp"] * 9/5) + 32;
$api["lat"]=$drive["latitude"];
$api["lng"]=$drive["longitude"];
return $api;
}