nhance/app/Controllers/FhplApiController.php

74 lines
2.0 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use App\Models\BatchFileModel;
use App\Models\EmployeePolicyModel;
use App\Models\TpaApiDataModel;
use App\Models\ClientPolicyModel;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\Jobs;
class FhplApiController extends BaseController
{
use ResponseTrait;
protected $db;
protected $fhplTpaId;
public function __construct()
{
$this->db = \Config\Database::connect();
$this->fhplTpaId = getenv('FHPL_PRIMARY_KEY_CONSTANT');
}
public function generateAuthToken()
{
$url = env('FHPL_TOKEN_URL'); // example: https://uat.fhpl.net/token
// x-www-form-urlencoded body
$postData = http_build_query([
'UserName' => 'TestApi@fhpl',
'Password' => 'Fhpl@12345',
'grant_type' => 'password',
]);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET', // SAME AS POSTMAN
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json',
],
CURLOPT_TIMEOUT => 30,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
return $this->response->setJSON([
'status' => false,
'error' => curl_error($ch),
]);
}
curl_close($ch);
return $this->response->setJSON([
'status' => $httpCode === 200,
'http_code' => $httpCode,
'response' => json_decode($response, true),
]);
}
}