Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ tesla-php

Tesla PHP 5+ library for REST API Interaction. Based on: http://docs.timdorr.apiary.io/

You will need to fill in the Client ID and Secret with a valid ID and Secret. Check the apiary docs for more info.


## Usage

Expand Down
118 changes: 76 additions & 42 deletions TeslaAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
class TeslaAPI
{

private $url = "https://portal.vn.teslamotors.com";
private $cookie_file = "/tmp/tesla-api-cookie";
private $clientID = "{CLIENT ID}"; // Get current Client ID and Secret from apiary
private $clientSecret = "{CLIENT SECRET}";
private $url = "https://owner-api.teslamotors.com";
private $id;
private $token;

/**
* @param $email
Expand All @@ -20,9 +22,7 @@ class TeslaAPI
public function __construct($email, $password, $id = 0)
{
$this->id = $id;
if (!is_file($this->cookie_file)) {
$this->api_auth($email, $password);
}
$this->api_auth($email, $password);
$this->api_validate_auth();
}

Expand All @@ -33,14 +33,15 @@ public function __construct($email, $password, $id = 0)
private function api_auth($email, $password)
{
try {
$params = http_build_query(array('user_session' => array('email' => $email, 'password' => $password)));
$ch = curl_init($this->url . "/login");
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file);
$params = http_build_query(array('grant_type' => 'password', 'client_id' => $this->clientID, 'client_secret' => $this->clientSecret, 'email' => $email, 'password' => $password));
$ch = curl_init($this->url . "/oauth/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$output = curl_exec($ch);
$data = json_decode($output, true);
$this->token = $data['access_token'];
} catch (Exception $e) {
die('API Auth Failed! Exception: ' . $e);
}
Expand All @@ -55,25 +56,25 @@ private function api_auth($email, $password)
public function api_call($command = "", $params = array())
{
if (empty($command)) {
$api_url = $this->url . "/vehicles";
$api_url = $this->url . "/api/1/vehicles";
} else {
if ($command === "mobile_enabled") {
$api_url = $this->url . "/vehicles/" . $this->id . "/mobile_enabled";
if (!count($params)) {
$api_url = $this->url . "/api/1/vehicles/" . $this->id . $command;
} else {
if (!count($params)) {
$api_url = $this->url . "/vehicles/" . $this->id . "/command/" . $command;
} else {
$http_params = http_build_query($params);
$api_url = $this->url . "/vehicles/" . $this->id . "/command/" . $command . "?" . $http_params;
}
$http_params = http_build_query($params);
$api_url = $this->url . "/api/1/vehicles/" . $this->id . $command . "?" . $http_params;
}
}

try {
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $this->token, "Accept: application/json"));
if (preg_match("/(command|wake_up)/", $command)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, null);
}
$output = curl_exec($ch);
} catch (Exception $e) {
die("API Call Failed! Exception: " . $e);
Expand All @@ -87,12 +88,11 @@ public function api_call($command = "", $params = array())
private function api_validate_auth()
{
$result = $this->api_call();
if (isset($result[0]['id']) && $result[0]['id'] > 0) {
if (isset($result['response'][0]['id_s']) && $result['response'][0]['id_s'] > 0) {
if ($this->id < 1) {
$this->id = $result[0]['id'];
$this->id = $result['response'][0]['id_s']; // id_s is a stringified version of id since the numeric id is now a BIGINT
}
} else {
unlink($this->cookie_file);
Throw New Exception('auth fail.');
}
}
Expand All @@ -110,135 +110,159 @@ public function get_vehicles()
*/
public function get_mobile_enabled()
{
return $this->api_call("mobile_enabled");
return $this->api_call("/mobile_enabled");
}

/**
* @return mixed
*/
public function get_charge_state()
{
return $this->api_call("charge_state");
return $this->api_call("/data_request/charge_state");
}

/**
* @return mixed
*/
public function get_climate_state()
{
return $this->api_call("climate_state");
return $this->api_call("/data_request/climate_state");
}

/**
* @return mixed
*/
public function get_drive_state()
{
return $this->api_call("drive_state");
return $this->api_call("/data_request/drive_state");
}

/**
* @return mixed
*/
public function get_gui_settings()
{
return $this->api_call("gui_settings");
return $this->api_call("/data_request/gui_settings");
}

/**
* @return mixed
*/
public function get_vehicle_state()
{
return $this->api_call("vehicle_state");
return $this->api_call("/data_request/vehicle_state");
}

/**
* @return mixed
*/
public function get_wake_up()
{
return $this->api_call("/wake_up");
}

/**
* @return mixed
*/
public function get_charge_port_door_open()
{
return $this->api_call("charge_port_door_open");
return $this->api_call("/command/charge_port_door_open");
}

/**
* @return mixed
*/
public function get_charge_standard()
{
return $this->api_call("charge_standard");
return $this->api_call("/command/charge_standard");
}

/**
* @return mixed
*/
public function get_charge_max_range()
{
return $this->api_call("charge_max_range");
return $this->api_call("/command/charge_max_range");
}

/**
* @return mixed
*/
public function get_charge_start()
{
return $this->api_call("charge_start");
return $this->api_call("/command/charge_start");
}

/**
* @return mixed
*/
public function get_charge_stop()
{
return $this->api_call("charge_stop");
return $this->api_call("/command/charge_stop");
}

/**
* @return mixed
*/
public function get_flash_lights()
{
return $this->api_call("flash_lights");
return $this->api_call("/command/flash_lights");
}

/**
* @return mixed
*/
public function get_honk_horn()
{
return $this->api_call("honk_horn");
return $this->api_call("/command/honk_horn");
}

/**
* @return mixed
*/
public function get_door_unlock()
{
return $this->api_call("door_unlock");
return $this->api_call("/command/door_unlock");
}

/**
* @return mixed
*/
public function get_door_lock()
{
return $this->api_call("door_lock");
return $this->api_call("/command/door_lock");
}

/**
* @return mixed
*/
public function get_auto_conditioning_start()
{
return $this->api_call("auto_conditioning_start");
return $this->api_call("/command/auto_conditioning_start");
}

/**
* @return mixed
*/
public function get_auto_conditioning_stop()
{
return $this->api_call("auto_conditioning_stop");
return $this->api_call("/command/auto_conditioning_stop");
}

/**
* @return mixed
*/
public function get_remote_start()
{
return $this->api_call("/command/remote_start_drive", array('password' => $this->password));
}

/**
* @return mixed
*/
public function get_trunk_open()
{
return $this->api_call("/command/trunk_open");
}

/**
Expand All @@ -247,7 +271,17 @@ public function get_auto_conditioning_stop()
public function get_google_map()
{
$drive_state = $this->get_drive_state();
return "http://maps.google.com/?q=" . $drive_state['latitude'] . "," . $drive_state['longitude'];
return "http://maps.google.com/?q=" . $drive_state['response']['latitude'] . "," . $drive_state['response']['longitude'];
}

/**
* @param $percent
*
* @return mixed
*/
public function set_charge_limit($percent = 75)
{
return $this->api_call("/command/set_charge_limit", array('percent' => $percent));
}

/**
Expand All @@ -258,7 +292,7 @@ public function get_google_map()
*/
public function set_set_temps($driver_temp, $pass_temp)
{
return $this->api_call("set_temps", array('driver_temp' => $driver_temp, 'passenger_temp' => $pass_temp));
return $this->api_call("/command/set_temps", array('driver_temp' => $driver_temp, 'passenger_temp' => $pass_temp));
}

/**
Expand All @@ -268,7 +302,7 @@ public function set_set_temps($driver_temp, $pass_temp)
*/
public function set_sun_roof_control($state = "close")
{
return $this->api_call("sun_roof_control", array('state' => $state));
return $this->api_call("/command/sun_roof_control", array('state' => $state));
}

}