run(); $this->getChaves(); } function run() { $db = Database::Singleton(); $this->db = $db->run(); return $this->db; } function getChaves(){ $query = $this->db->prepare("SELECT * FROM `platform_module_auth_apis` WHERE id=1"); $query->execute(); if ($query->rowCount() > 0) { $query = $query->fetch(); $this->apiKey = $query["egoi_token"]; $this->listID = $query["egoi_list_id"]; } } private function curlConection( $link, $values, $method = "POST" ){ $url = 'https://api.egoiapp.com/' . $link; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_POSTFIELDS => $values, CURLOPT_HTTPHEADER => array( "Apikey: " . $this->apiKey, "Content-Type: application/json", "accept: application/json", ), )); $response = curl_exec($curl); $response = json_decode($response,true); if( $response == null ){ sleep(1); $response = $this->curlConection($link, $values, $method); } curl_close($curl); return $response; } public function emailUpdate($dados){ /*var_dump($this->apiKey); var_dump($this->listID);*/ $url = 'lists/' . $this->listID . '/contacts'; $json = json_encode([ "base" => array( "status" => 'active', "email" => $dados["email"], "language" => "pt", ) ]); $response = $this->curlConection($url, $json); if( !empty($response["errors"]["unique_field_in_use"]) ){ $url = 'lists/' . $this->listID . '/contacts/' . $response["errors"]["contacts"][0]; $response = $this->curlConection($url, $json, "PATCH"); } return $response; } public function searchContact($email){ $url = 'contacts/search?type=email&contact=' . $email; $response = $this->curlConection($url, "", "GET"); return $response; } public function getAllTags(){ $url = 'tags?offset=0&limit=60&order=desc&order_by=tag_id'; $response = $this->curlConection($url, "", "GET"); return $response; } public function createTag($tag){ $url = 'tags'; $json = json_encode(["name" => $tag,"color" => "#FFFFFF"]); $response = $this->curlConection($url, $json); return $response; } public function checkTag($tag){ $url = 'tags?offset=0&limit=60&order=desc&order_by=tag_id'; $response = $this->curlConection($url, "", "GET"); $existe = false; foreach( $response["items"] as $item ){ if( $item["name"] == $tag ){ $existe = $item["tag_id"]; break; } } if( $existe == false ){ $url = 'tags'; $json = json_encode(["name" => $tag,"color" => "#FFFFFF"]); $response = $this->curlConection($url, $json); $existe = $response["tag_id"]; } return $existe; } public function addTag($tag, $email){ $tag_id = $this->checkTag($tag); $contact_id = $this->searchContact($email); $url = 'lists/' . $this->listID . '/contacts/actions/attach-tag'; $json = json_encode([ "tag_id" => $tag_id, "contacts" => [ $contact_id["items"][0]["contact_id"] ] ]); $response = $this->curlConection($url, $json); } public function addTagCustom($tag_id, $contact_id){ $url = 'lists/' . $this->listID . '/contacts/actions/attach-tag'; $json = json_encode([ "tag_id" => $tag_id, "contacts" => [$contact_id] ]); $response = $this->curlConection($url, $json); } public function removeTag($tag_id, $email){ $contact_id = $this->searchContact($email); $url = 'lists/' . $this->listID . '/contacts/actions/detach-tag'; $json = json_encode([ "tag_id" => $tag_id, "contacts" => [ $contact_id["items"][0]["contact_id"] ] ]); $response = $this->curlConection($url, $json); } public function emailUpdateEmail($json, $email){ $contact_id = $this->searchContact($email); $contact_id = $contact_id["items"][0]["contact_id"]; $url = 'lists/' . $this->listID . '/contacts/' . $contact_id; $response = $this->curlConection($url, $json, "PATCH"); return $response; } public function getContact($contact_id){ $url = 'lists/' . $this->listID . '/contacts/' . $contact_id; $response = $this->curlConection($url, "", "GET"); return $response; } public function getContactId($email){ $contact_id = $this->searchContact($email); $response = $contact_id["items"][0]["contact_id"]; return $response; } public function getTagsFromContact($email){ $contact_id = $this->searchContact($email); $contact_id = $contact_id["items"][0]["contact_id"]; $contact_info = $this->getContact($contact_id); return $contact_info["tags"]; } } ?>run(); $this->getChaves(); } function run() { $db = Database::Singleton(); $this->db = $db->run(); return $this->db; } function getChaves(){ $query = $this->db->prepare("SELECT * FROM `platform_module_auth_apis` WHERE id=1"); $query->execute(); if ($query->rowCount() > 0) { $query = $query->fetch(); $this->apiKey = $query["mailchimp_token"]; $this->listID = $query["mailchimp_list_id"]; } } private function buildListMemberURL($email) { $memberID = md5(strtolower($email)); $dataCenter = substr($this->apiKey, strpos($this->apiKey, '-') + 1); return "https://{$dataCenter}.api.mailchimp.com/3.0/lists/{$this->listID}/members/{$memberID}"; } public function subscribeToList($email) { $url = $this->buildListMemberURL($email); $json = json_encode([ 'email_address' => $email, 'status' => 'subscribed' ]); $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $this->apiKey); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); $resultMC = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $response = array(); if($httpCode == 200) { $result = json_decode($resultMC, true); $response = $result['status']; } return $response; } } ?>SQLSTATE[28000] [1045] Access denied for user 'miterap_site'@'localhost' (using password: YES)