This repository has been archived on 2023-09-26. You can view files and clone it, but cannot push or open issues or pull requests.
php-Plex-OAuth/Plex.php

70 lines
1.7 KiB
PHP

<?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"];
}
}
?>