kibioctet

API JV WiP

Jul 23rd, 2022
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.70 KB | None | 0 0
  1. <?php
  2. class Jv {
  3.     private int $apiVersion = 4;
  4.     private string $subdomain = "api", $partnerKey = "550c04bf5cb2b", $hmacSecret = "d84e9e5f191ea4ffc39c22d11c77dd6c", $coniunctio = "";
  5.    
  6.     /**
  7.      * Constructeur
  8.      *
  9.      * @param string $username Nom d'utilisateur
  10.      * @param string $password Mot de passe
  11.      */
  12.     public function __construct(string $username, string $password) {
  13.         $this->curl = curl_init();
  14.         curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
  15.         curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
  16.         curl_setopt($this->curl, CURLOPT_USERAGENT, "JeuxVideo-Android/267");
  17.         curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  18.        
  19.         $this->login($username, $password);
  20.     }
  21.    
  22.     /**
  23.      * Destructeur
  24.      */
  25.     public function __destruct() {
  26.         $this->logout();
  27.         curl_close($this->curl);
  28.     }
  29.    
  30.     /**
  31.      * Lance un appel vers un path de l'API JV
  32.      *
  33.      * @param string $path Path
  34.      * @param string $method Méthode HTTP
  35.      * @param array $post Données POST
  36.      * @param bool $enableHeaders Retourne ou non les headers HTTP (et désactive en conséquence l'encodage en JSON)
  37.      */
  38.     private function call(string $path, string $method = "GET", array $post = [], bool $enableHeaders = false) {
  39.         $date = date("c");
  40.         $signature = hash_hmac("sha256", "{$this->partnerKey}\n$date\n$method\n{$this->subdomain}.jeuxvideo.com\n/v{$this->apiVersion}/$path\n", $this->hmacSecret);
  41.         $header = "PartnerKey={$this->partnerKey}, Signature=$signature, Timestamp=$date";
  42.        
  43.         curl_setopt($this->curl, CURLOPT_URL, "https://{$this->subdomain}.jeuxvideo.com/v{$this->apiVersion}/$path");
  44.         curl_setopt($this->curl, CURLOPT_HTTPHEADER, ["Jvc-Authorization: $header", "Content-Type: application/json"]);
  45.         curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
  46.         if ($method != "GET") {
  47.             curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($post));
  48.         } else {
  49.             curl_setopt($this->curl, CURLOPT_POST, false);
  50.         }
  51.         if (!empty($this->coniunctio)) {
  52.             curl_setopt($this->curl, CURLOPT_COOKIE, "coniunctio={$this->coniunctio}");
  53.         }
  54.         curl_setopt($this->curl, CURLOPT_HEADER, $enableHeaders);
  55.        
  56.         return !$enableHeaders ? json_decode(curl_exec($this->curl)) : curl_exec($this->curl);
  57.     }
  58.    
  59.     /**
  60.      * Se connecte à un compte JV
  61.      *
  62.      * @param string $username Nom d'utilisateur
  63.      * @param string $password Mot de passe
  64.      *
  65.      * @return bool Résultat
  66.      */
  67.     private function login(string $username, string $password) : bool {
  68.         $result = $this->call("accounts/login", "POST", ["alias" => $username, "password" => $password], true);
  69.        
  70.         if (!strstr($result, "coniunctio=")) {
  71.             trigger_error("Impossible de récupérer le cookie d'authentification.", E_USER_ERROR);
  72.         }
  73.        
  74.         $this->coniunctio = explode("coniunctio=", explode(";", $result)[0])[1];
  75.        
  76.         return true;
  77.     }
  78.    
  79.     /**
  80.      * Se déconnecte d'un compte JV (détruit en conséquence le coniunctio)
  81.      *
  82.      * @return bool Résultat
  83.      */
  84.     private function logout() : bool {
  85.         if (empty($this->coniunctio)) {
  86.             return false;
  87.         }
  88.        
  89.         $this->call("accounts/logout", "POST");
  90.        
  91.         return true;
  92.     }
  93.    
  94.     /**
  95.      * Charge les données d'un profil JV
  96.      *
  97.      * @param int $accountId ID du compte
  98.      *
  99.      * @return stdClass Résultat
  100.      */
  101.     public function loadProfile(int $accountId) : stdClass {
  102.         return $this->call("accounts/$accountId/profile");
  103.     }
  104.    
  105.     /**
  106.      * Charge les contributions d'un profil JV
  107.      *
  108.      * @param int $accountId ID du compte
  109.      *
  110.      * @return stdClass Résultat
  111.      */
  112.     public function loadProfilePage(int $accountId) : stdClass {
  113.         return $this->call("accounts/$accountId/page");
  114.     }
  115.    
  116.     /**
  117.      * Charge les "contenus" d'un profil JV
  118.      *
  119.      * @param int $accountId ID du compte
  120.      * @param int $page Page
  121.      * @param int $perPage Nombre d'entrées par page
  122.      *
  123.      * @return stdClass Résultat
  124.      */
  125.     public function loadProfileContents(int $accountId, $page = 1, $perPage = 10) : stdClass {
  126.          return $this->call("accounts/$accountId/page/contents");
  127.      }
  128.      
  129.      /**
  130.       * Charge les reviews d'un profil JV
  131.       *
  132.      * @param int $accountId ID du compte
  133.      * @param int $page Page
  134.      * @param int $perPage Nombre d'entrées par page
  135.      *
  136.      * @return stdClass Résultat
  137.      */
  138.     public function loadProfileReviews(int $accountId, $page = 1, $perPage = 10) : stdClass {
  139.          return $this->call("accounts/$accountId/page/reviews");
  140.      }
  141.      
  142.     /**
  143.      * Modifie la description du compte
  144.      *
  145.      * @param string $description Description
  146.      *
  147.      * @return bool Résultat
  148.      */
  149.     public function changeDescription(string $description = "") : bool {
  150.         $result = $this->call("accounts/me/profile/description", "PUT", ["description" => $description]);
  151.        
  152.         if (isset($result->message)) {
  153.             trigger_error($result->message, E_USER_ERROR);
  154.         }
  155.        
  156.         return true;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment