我正在通過PHP腳本更新我的DNS。我已經看過與cURL相關的API文檔,所以我試圖將cURL psot轉換爲PHP。通過PHP/Curl使用CloudFlare API返回錯誤
我有以下代碼:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/<MY_ZONE>/dns_records/<MY_ID>");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$fields = array();
$fields["X-Auth-Email"] = "[email protected]";
$fields["X-Auth-Key"] = "MY_KEY";
$fields["Content-Type"] = "application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $fields);
$dnsData = array();
$dnsData["id"] = "MY_ID";
$dnsData["type"] = "A";
$dnsData["name"] = "home";
$dnsData["content"] = $newIPAddress;
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dnsData));
echo "posting to API<br />";
$result = curl_exec($ch);
echo "Result: " . $result;
與上面的代碼我收到以下響應從CloudFlare的回來。
{ 「成功」:假, 「錯誤」:[{ 「代碼」:6003, 「消息」: 「無效請求 頭」, 「error_chain」:[{ 「代碼」:6100,「信息「:」Missing X-Auth-Email header「},{」code「:6101,」message「:」Missing X-Auth-Key header「},{」code「:6105,」message「:」Invalid Content-Type頭,有效 值 應用/ JSON,多部分/格式數據 「}]}],」 消息 「:[],」 結果「:空}
我試圖改變json_encode改爲http_build_query,但兩者都返回相同的錯誤。
感謝您提供的任何幫助。