Created
August 29, 2024 14:16
-
-
Save SmileYzn/bc84844333a7119207d554d789ef62f4 to your computer and use it in GitHub Desktop.
Spoten App Class
This file contains hidden or 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 | |
| class SpotenApp | |
| { | |
| public $apiType = "dev"; | |
| public $apiUrl = ["homolog" => "https://dashboard-homolog.spoten.app/api", "producao" => "https://dashboard.spoten.app/api"]; | |
| public $apiTimeout = 5; | |
| public $apiLastRequest = []; | |
| public $companyId = 267; | |
| public $email = ""; // Your email | |
| public $password = ""; // Your password | |
| public $token = ""; | |
| public function __construct($companyId, $email, $password, $apiType = "homolog") | |
| { | |
| $this->companyId = $companyId; | |
| $this->apiType = $apiType; | |
| $this->email = $email; | |
| $this->password = $password; | |
| $this->login(); | |
| } | |
| public function getLastRequest() | |
| { | |
| return $this->apiLastRequest; | |
| } | |
| public function login() | |
| { | |
| $this->POST("/auth/login", ["email" => $this->email, "password" => $this->password]); | |
| if (!empty($this->apiLastRequest['token'])) | |
| { | |
| $this->token = $this->apiLastRequest['token']; | |
| } | |
| } | |
| public function store($title, $description, $photo) | |
| { | |
| if (!empty($title)) | |
| { | |
| if (file_exists($photo)) | |
| { | |
| $photo = 'data:image/' . pathinfo($photo, PATHINFO_EXTENSION) . ';base64,' . base64_encode(file_get_contents($photo)); | |
| } | |
| $this->POST("/companies/{$this->companyId}/news", ["title" => $title, "description" => $description, "photo" => $photo]); | |
| } | |
| } | |
| public function POST($url, $data) | |
| { | |
| if (!empty($url) && !empty($data)) | |
| { | |
| $ch = curl_init(); | |
| curl_setopt_array($ch, | |
| [ | |
| CURLOPT_HTTPHEADER => | |
| [ | |
| "Content-Type: application/json", | |
| "X-Holding-Id: 1", | |
| ($this->token) ? "Authorization: Bearer {$this->token}" : "", | |
| ], | |
| CURLOPT_ENCODING => "", | |
| CURLOPT_FOLLOWLOCATION => TRUE, | |
| CURLOPT_SSL_VERIFYPEER => FALSE, | |
| CURLOPT_SSL_VERIFYHOST => FALSE, | |
| CURLOPT_POST => TRUE, | |
| CURLOPT_RETURNTRANSFER => TRUE, | |
| CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
| CURLOPT_TIMEOUT => $this->apiTimeout, | |
| CURLOPT_URL => "{$this->apiUrl[$this->apiType]}{$url}", | |
| CURLOPT_POSTFIELDS => json_encode(array_map("utf8_encode", $data)), | |
| ]); | |
| $this->apiLastRequest = curl_exec($ch); | |
| curl_close($ch); | |
| $this->apiLastRequest = json_decode($this->apiLastRequest, true); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment