Cloudflare API问题放置请求(“代码”:9020,“消息”:“无效的DNS记录类型”)

时间:2019-01-31 05:12:38

标签: php api curl dns cloudflare

使用cloudflare APIv4时出现问题。尝试更新DNS记录,但不确定为什么收到以下错误:

  

{“成功”:false,“错误”:[{“代码”:1004,“消息”:“ DNS验证错误”,“ error_chain”:[{“代码”:9020,“消息”:“无效” DNS记录类型 “}]}],” 消息 “:[],” 结果“:空}

下面是PHP函数:

function updateCloudflareDNS($zone_id,$dns_id, $updatedata){
 $updatedata = '[{"name":"**.****.com"},{"type":"A"},{"ttl":"1"},{"content":"8.8.8.8"},{"proxied":"true"}]';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/".$zone_id."/dns_records/".$dns_id);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json; charset=utf-8',
    'X-Auth-Email: **********',
    'X-Auth-Key: ***********'
));    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $updatedata);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
$response = curl_exec($ch);
curl_close($ch);
return $response;
}

此外,我按照Cloudflare API documentation

的说明正确输入了记录类型“ A”

有人可以帮我解决这个问题吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您要发送的有效载荷为:

  

[{"name":"**.****.com"},{"type":"A"},{"ttl":"1"},{"content":"8.8.8.8"},{"proxied":"true"}]

API的期望如下:

  

{"type":"A", "name":"example.com", "content":"127.0.0.1", "ttl":120, "priority":10,"proxied":false}

这是在PHP中正确构造JSON的方式:

$POST = json_encode(array(
    "type" => "A",
    "name" => "...",
    ...
));