CloudFlare DDNS Script (PHP)

  1. <?php 
  2. // PHP CloudFlare IP Updater v4 
  3. // When you need DDNS with CloudFlare.com 
  4. // (c) 2018 anggit.com 
  5. if (!(isset($_GET['user'], $_GET['pass']) 
  6.     && $_GET['user'] === 'user' 
  7.     && $_GET['pass'] === 'pass' 
  8.     )) { 
  9.         exit; 
  10.     } 
  11. $headers = array( 
  12.     'X-Auth-Email: example@gmail.com', 
  13.     'X-Auth-Key: 5d59f22ef88d9b6351d529e5c2c23156fdf3b, 
  14.     'Content-Type: application/json' 
  15. ); 
  16. $url  = 'https://api.cloudflare.com/client/v4/zones/504f0c70c8577b41c1d18bd0eff98a47/dns_records'; 
  17. $host = 'homeserver.anggit.com'; 
  18. // Stop edit if you are in doubt 
  19. $ip = $_SERVER['REMOTE_ADDR']; 
  20. if (file_exists('ip.txt')) { 
  21.     if ($ip === file_get_contents('ip.txt')) { // IP address has not changed. No need to update :) 
  22.         exit; 
  23.     } 
  24.     else { 
  25.         file_put_contents('ip.txt', $ip); 
  26.     } 
  27. } 
  28. else { 
  29.     file_put_contents('ip.txt', $ip); 
  30. } 
  31. $ch = curl_init(); 
  32. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  35. curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
  36. curl_setopt($ch, CURLOPT_URL, $url.'?name='.$host); 
  37. $contents = curl_exec($ch); 
  38. if (!empty($contents)) { 
  39.     $data = json_decode($contents, true); 
  40.     curl_setopt($ch, CURLOPT_URL, $url.'/'.$data['result'][0]['id']); 
  41.     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 
  42.     $params = array( 
  43.         'type' => 'A', 
  44.         'name' => $host, 
  45.         'content' => $ip, 
  46.         'ttl' => 1, 
  47.         'proxied' => false 
  48.     ); 
  49.     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); 
  50.     curl_exec($ch); 
  51. } 
  52. curl_close($ch);