This commit is contained in:
Tyler Perkins 2022-12-27 22:55:11 -05:00
parent 85c6a8509a
commit 29e3563555
2 changed files with 80 additions and 0 deletions

69
Plex.php Normal file
View File

@ -0,0 +1,69 @@
<?php
class PlexAuthenticator {
// Product Name. Shown when loggin in
private $plexProduct;
// Client ID, this should be a UUID, we can randomly generate one
private $clientID;
// The token we get back
private $token;
private $id;
private $code;
public static function makeNewClient(String $productName){
return new PlexAuthenticator($productName, uniqid());
}
public static function makeNewClientWithId(String $productName, String $clientName){
return new PlexAuthenticator($productName, $clientName);
}
private function __construct(String $productName, String $clientName) {
$this->clientID = $clientName;
$this->plexProduct = $productName;
}
private function getPins() {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://plex.tv/api/v2/pins");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$headers = array("Content-Type: application/x-www-form-urlencoded", "accept: application/json");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = "strong=true&X-Plex-Product=" . $this->plexProduct . "&X-Plex-Client-Identifier=" . $this->clientID;
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($curl);
$returnCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
curl_close($curl);
if($returnCode != 201)
return false;
return $resp;
}
public function getURL(){
$pinJSON = $this->getPins();
if(!$pinJSON){
return false;
}
$parsedPinJSON = json_decode($pinJSON,true);
$this->id = parsedPinJSON["id"];
$this->code = parsedPinJSON["code"];
}
}
?>

11
index.php Normal file
View File

@ -0,0 +1,11 @@
<!-- This file exists for testing and is not part of the library -->
<?php
include 'Plex.php';
$c = PlexAuthenticator::makeNewClient("Clortox-Test-Product");
var_dump($c->getURL());
?>