Filippo Bertilotti
9 days ago c52de027ae84a21cdf03c392b29734793bedff6a
commit | author | age
9f6455 1 <?php
DC 2 class VolaRestMailAPI
3 {
4
5     private $serviceUri;
6     private $username;
7     private $password;
8     private $login_request_api_endpoint;
9     private $mail_request_api_endpoint;
10
11     public function __construct()
12     {
13         $this->serviceUri = "localhost:8000/api";
14         //$this->serviceUri = "https://volamailtool-local.nginx-apps-ns:4448/api";
15         $this->username = "vola01";
16         $this->password = "Password01";
17         $this->login_request_api_endpoint = "/auth/login";
18         $this->mail_request_api_endpoint = "/mail/request";
19         $this->ssl_verify_host = 0;
20         $this->ssl_verify_peer = false;
21     }
22
23     public function mailRequest($params)
24     {
25         try {
26             $mapping = [
27                 "template_mail_id" => $params["template_mail"],
28                 "template_mail_params" => $params["template_mail_params"],
29                 "recipient_to" => [$params["registration_email"]],
30                 "recipient_cc" => [],
31                 "template_pdf_params" => (isset($params["template_pdf_params"])) ? $params["template_pdf_params"] : array(),
32                 "attached_files" => (isset($params["attached_files"])) ? $params["attached_files"] : array(),
33                 //"optional_pdf_group" => "test_group",
34             ];
35
36             $token = $this->login();
37             if ($token) {
38                 return ["result" => true, "idreq" => $this->call($token, $mapping)];
39             }
40             return ["result" => false];
41
42         } catch (Exception $e) {
43             error_log("Errore nell'esecuzione della chiamata a Vola Rest Mail Tool:\r".$e->getTraceAsString());
44             return ["result" => false];
45         }
46     }
47
48     private function login()
49     {
50         $curl = curl_init();
51
52         curl_setopt_array($curl, array(
53             CURLOPT_URL => $this->serviceUri.$this->login_request_api_endpoint,
54             CURLOPT_RETURNTRANSFER => true,
55             CURLOPT_ENCODING => '',
56             CURLOPT_MAXREDIRS => 10,
57             CURLOPT_TIMEOUT => 0,
58             CURLOPT_FOLLOWLOCATION => true,
59             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
60             CURLOPT_CUSTOMREQUEST => 'POST',
61             CURLOPT_POSTFIELDS => json_encode(["username"=>$this->username,"password"=>$this->password]),
62             CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
63             CURLOPT_SSL_VERIFYHOST => $this->ssl_verify_host,
64             CURLOPT_SSL_VERIFYPEER => $this->ssl_verify_peer,
65         ));
66
67         $response = curl_exec($curl);
68         curl_close($curl);
69
70         if (isset($response)) {
71             $parsing = json_decode($response);
72             if (isset($parsing->token)) {
73                 return $parsing->token;
74             } else {
75                 error_log("Token non ricevuto. Risposta:\r" . print_r($response, 1));
76                 return false;
77             }
78         }
79         error_log("Si è verificato un errore nella chiamata di login");
80         return false;
81     }
82
83     private function call($token, $data)
84     {
85         $curl = curl_init();
86
87         curl_setopt_array($curl, array(
88             CURLOPT_URL => $this->serviceUri.$this->mail_request_api_endpoint,
89             CURLOPT_RETURNTRANSFER => true,
90             CURLOPT_ENCODING => '',
91             CURLOPT_MAXREDIRS => 10,
92             CURLOPT_TIMEOUT => 0,
93             CURLOPT_FOLLOWLOCATION => true,
94             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
95             CURLOPT_CUSTOMREQUEST => 'POST',
96             CURLOPT_POSTFIELDS => json_encode($data),
97             CURLOPT_HTTPHEADER => array(
98                 'Authorization: Bearer '.$token,
99                 'Content-Type: application/json',
100             ),
101             CURLOPT_SSL_VERIFYHOST => $this->ssl_verify_host,
102             CURLOPT_SSL_VERIFYPEER => $this->ssl_verify_peer,
103         ));
104
105         $response = curl_exec($curl);
106         curl_close($curl);
107
108         if (isset($response)) {
109             $parsing = json_decode($response);
110             if (isset($parsing->result)) {
111                 return $parsing->result;
112             } else {
113                 error_log("Id Req non ricevuto. Risposta:\r" . print_r($response, 1));
114                 return false;
115             }
116         }
117         error_log("Si è verificato un errore nella chiamata di richiesta mail");
118         return false;
119     }
120
121 }