文件
parlz-domain/aliapi/api/add_record.php
T

60 行
1.8 KiB
PHP

<?php
require_once '../config.php';
require_once '../lib/AliyunDnsClient.php';
header('Content-Type: application/json; charset=utf-8');
$config = require '../config.php';
$allowedIps = $config['api']['allowed_ips'];
$clientIp = $_SERVER['REMOTE_ADDR'];
if ($allowedIps !== '*' && !in_array($clientIp, $allowedIps)) {
echo json_encode(['code' => 403, 'message' => 'Access denied']);
exit;
}
$token = $_POST['token'] ?? $_GET['token'] ?? null;
if ($token !== $config['api']['token']) {
echo json_encode(['code' => 403, 'message' => 'Invalid token']);
exit;
}
$domainName = $_POST['domain_name'] ?? $_GET['domain_name'] ?? null;
$rr = $_POST['rr'] ?? $_GET['rr'] ?? null;
$type = $_POST['type'] ?? $_GET['type'] ?? 'A';
$value = $_POST['value'] ?? $_GET['value'] ?? null;
$ttl = intval($_POST['ttl'] ?? $_GET['ttl'] ?? 600);
$priority = $_POST['priority'] ?? $_GET['priority'] ?? null;
if ($priority !== null) $priority = intval($priority);
if (!$domainName || !$rr || !$value) {
echo json_encode(['code' => 400, 'message' => 'Missing required parameters: domain_name, rr, value']);
exit;
}
try {
$client = new AliyunDnsClient(
$config['aliyun']['access_key_id'],
$config['aliyun']['access_key_secret'],
$config['aliyun']['endpoint'],
$config['aliyun']['region_id']
);
$result = $client->addDomainRecord($domainName, $rr, $type, $value, $ttl, $priority);
echo json_encode([
'code' => 200,
'message' => 'Success',
'data' => [
'record_id' => $result['RecordId'] ?? null,
'rr' => $rr,
'type' => $type,
'value' => $value,
'ttl' => $ttl
]
]);
} catch (Exception $e) {
echo json_encode(['code' => 500, 'message' => $e->getMessage()]);
}
?>