base_url = $base_url; $this->username = $username; $this->password = $password; $this->hash = $this->generateHash(); } public function createApplication($notif_url, $client_info, $client_contact_info, $car_info) { $body = [ 'notif_url' => $notif_url, 'client_info' => $client_info, 'client_contact_info' => $client_contact_info, 'car_info' => $car_info, ]; return $this->doRequest('/api/v1/ctpl/applications', true, $body); } public function tagApplicationPaid($application_id) { $url = '/api/v1/ctpl/application/' . $application_id . '/paid'; return $this->doRequest($url, true); } public function getVehicleMakers() { return $this->doRequest('/api/v1/ctpl/vehicle-makers'); } public function getVehicleModels() { return $this->doRequest('/api/v1/ctpl/vehicle-models'); } public function getVehicleTrims() { return $this->doRequest('/api/v1/ctpl/vehicle-trims'); } protected function generateHash() { return base64_encode($this->username . ":" . $this->password); } protected function doRequest($url, $is_post = false, $body = []) { $curl = curl_init(); $options = [ CURLOPT_URL => $this->base_url . '/' . $url, CURLOPT_POST => $is_post, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Basic ' . $this->hash, ], ]; // add post body if present if (!empty($body)) { $options[CURLOPT_POSTFIELDS] = json_encode($body); } curl_setopt_array($curl, $options); $res = curl_exec($curl); curl_close($curl); error_log('Insurance API connector'); error_log(print_r($options, true)); error_log($res); // response return $this->handleResponse($res); } protected function handleResponse($res) { $inv_res = json_decode($res, true); // make sure result is always an array if ($inv_res == null) return []; return $inv_res; } }