Skip to content

Instantly share code, notes, and snippets.

@hctilg
Created May 18, 2024 19:43
Show Gist options
  • Save hctilg/f07d83bde94a56aa3764330f0b6624ae to your computer and use it in GitHub Desktop.
Save hctilg/f07d83bde94a56aa3764330f0b6624ae to your computer and use it in GitHub Desktop.
<?php
// checking the exists Library.
if (!file_exists("finglish.php")) {
copy('https://raw.githubusercontent.com/hctilg/finglish/main/index.php', 'finglish.php');
}
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$response = [];
if (trim($_REQUEST['text'] ?? '') === '') {
$response['ok'] = false;
$response['error_code'] = 400;
$response['message'] = 'text is empty';
} else {
$input = $_REQUEST['text'];
try {
$response['ok'] = true;
require_once('finglish.php');
$model = new Finglish();
$response['result'] = $model($input);
} catch (Exception $e) {
$response['ok'] = false;
$response['error_code'] = $e->getCode();
$response['message'] = $e->getMessage();
}
}
header_remove('X-Powered-By');
http_response_code($response['error_code'] ?? 200);
echo json_encode($response, 448);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment