Created
July 21, 2019 18:04
-
-
Save NickHatBoecker/a54704a0144cdba95dd78298605ebdde to your computer and use it in GitHub Desktop.
Bypass Philipps Hue HTTPS Certificate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This script is used to bypass the certificate error of philipps hue api when working with Javascript. | |
* You have to set an environment variable HUE_API_BASE_URL with the Hue Bridge IP and your hue userid. | |
* | |
* Example: https://192.168.0.1/api/hgdjklhKHFJLDSKHLHJFHKWHLwkhljl/lights | |
*/ | |
class HueApiHelper | |
{ | |
const METHOD_DELETE = 'DELETE'; | |
const METHOD_GET = 'GET'; | |
const METHOD_POST = 'POST'; | |
const METHOD_PUT = 'PUT'; | |
/** @var string **/ | |
private $apiBaseUrl; | |
/** @var string **/ | |
private $requestEndpoint; | |
/** @var string **/ | |
private $requestMethod; | |
/** @var array **/ | |
private $requestParameters; | |
/** | |
* @param string $requestEndpoint | |
* @param string $requestMethod | |
* @param array $requestParameters | |
*/ | |
public function __construct($requestEndpoint, $requestMethod, $requestParameters) | |
{ | |
$this->apiBaseUrl = getenv('HUE_API_BASE_URL'); | |
if (!$this->apiBaseUrl) { | |
$this->sendErrorResponse('HUE_API_BASE_URL not found.'); | |
exit; | |
} | |
$this->requestEndpoint = $requestEndpoint; | |
$this->requestMethod = $requestMethod; | |
$this->requestParameters = $requestParameters; | |
try { | |
$response = $this->sendRequest(); | |
$this->sendSuccessResponse($response); | |
} catch (\Exception $e) { | |
$this->sendErrorResponse($e->getMessage()); | |
} | |
} | |
public function sendRequest() | |
{ | |
$url = sprintf("%s%s", $this->apiBaseUrl, $this->requestEndpoint); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->requestMethod); | |
if ($this->requestMethod != self::METHOD_GET) { | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->requestParameters)); | |
} | |
$response = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); | |
$error = curl_error($ch); | |
curl_close($ch); | |
if ($httpCode === 200) { | |
return $response; | |
} | |
throw new \Exception(sprintf('ERROR: %s', $error)); | |
} | |
/** | |
* @param string $message | |
*/ | |
public function sendSuccessResponse($data) | |
{ | |
header("HTTP/1.1 200 OK"); | |
header('Content-Type: application/json'); | |
echo $data; | |
} | |
/** | |
* @param string $message | |
*/ | |
public static function sendErrorResponse($data) | |
{ | |
header("HTTP/1.1 400 Bad Request"); | |
header('Content-Type: application/json'); | |
echo json_encode($data); | |
} | |
} | |
$requestEndpoint = isset($_GET['endpoint']) ? $_GET['endpoint'] : ''; | |
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : HueApiHelper::METHOD_GET; | |
if ($method == HueApiHelper::METHOD_PUT || $method == HueApiHelper::METHOD_DELETE) { | |
$parameters = json_decode(file_get_contents("php://input"), true); | |
} elseif ($method == HueApiHelper::METHOD_POST) { | |
$parameters = $_POST; | |
} else { | |
$parameters = []; | |
} | |
if ($requestEndpoint) { | |
new HueApiHelper($requestEndpoint, $method, $parameters); | |
} else { | |
HueApiHelper::sendErrorResponse('No endpoint given'); | |
} | |
exit; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment