Files
tools/.hta_slug/dns-tools-get-mx-record.php
2026-01-20 16:26:36 +05:30

72 lines
1.4 KiB
PHP

<?php
/**
* DNS MX Record Lookup API
* Endpoint: /dns-tools-get-mx-record
* Method: POST
* Content-Type: application/json
*/
header('Content-Type: application/json; charset=utf-8');
// Allow only POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode([
'success' => false,
'message' => 'Only POST method allowed'
]);
exit;
}
// Read JSON input
$input = json_decode(file_get_contents('php://input'), true);
$domain = $input['domain'] ?? '';
// Domain validation
function validateDomain(string $domain): bool
{
return (bool) preg_match(
'/^(?!-)(?:[a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,}$/',
$domain
);
}
if (!$domain || !validateDomain($domain)) {
http_response_code(400);
echo json_encode([
'success' => false,
'message' => 'Invalid domain'
]);
exit;
}
// Fetch MX records
$records = dns_get_record($domain, DNS_MX);
$mxRecords = [];
if ($records !== false) {
foreach ($records as $record) {
$mxRecords[] = [
'host' => $record['target'] ?? '',
'priority' => $record['pri'] ?? null
];
}
}
// Response
if (empty($mxRecords)) {
echo json_encode([
'success' => false,
'domain' => $domain,
'message' => 'No MX records found',
'records' => []
]);
exit;
}
echo json_encode([
'success' => true,
'domain' => $domain,
'records' => $mxRecords
]);