TransIP DDNS

1 minute read

Today I made a new PHP script using the TransIP API to update DNS entries. This way it’s possible to automatically update your (dynamic) home IP address for example.

To use this script you need a server with a static IP address and PHP support. If you don’t have a server with a static IP address, free hosting will do the job.

Follow these steps to install this script:

  1. Download the TransIP API to your server
  2. Go to the TransIP CP (My account > API settings)
  3. Enable the API and add your server’s IP address to the list
  4. Add a new Key Pair and copy the key to your clipboard
  5. Edit Transip/ApiSettings.php and set $login and $privateKey (the key you just copied) Alternatively you can set this in the script itself
  6. Download the script below to your server (dns.php)
  7. Edit the settings in dns.php (domain, key, and records to update)
    This key is used to identify the client, this way the DNS won’t be updated with the IP address of someone else
    Do NOT use the same key as your TransIP API key, the use of a 25 characters long A-Z a-z 0-9 key is recommended
  8. Now, on the client, access dns.php on the server to update the DNS entries
    On Linux you can create a cronjob with the following command (don’t forget to replace the key):
    wget --spider http://example.com/dns.php?key=69l67Le20e819360d3YHO1175
    

You’re done. You can test if the script works by openening the URL in your browser and then check if the values are updated in the TransIP CP.


<?php
// SETTINGS
// The domain to edit
define('DOMAIN', 'example.com');

// The authentication key
define('KEY', '69l67Le20e819360d3YHO1175');

// The DNS entries to update (name => content)
// This script will NOT add new entries
$newValues = [
  'www' => $_SERVER['REMOTE_ADDR'],
];

// SCRIPT
// Exit if the key does not match
if($_GET['key'] != KEY) exit();

// Include the required files from the API
require_once('Transip/DomainService.php');

// Get the current DNS entries from TransIP
$dnsEntries = Transip_DomainService::getInfo(DOMAIN)->dnsEntries;

// Check every DNS entry
foreach($dnsEntries as $dnsEntry)
{
  // Check if the entry has to be updated and update it
  if(array_key_exists($dnsEntry->name, $newValues)) $dnsEntry->content = $newValues[$dnsEntry->name];
}

try
{
  // Commit the changes to the TransIP DNS servers
  Transip_DomainService::setDnsEntries(DOMAIN, $dnsEntries);
  // Done
  echo 'DNS updated.';
}
catch(SoapFault $f)
{
  // An error occured
  echo 'DNS not updated. ' . $f->getMessage();
}
?>

Updated:

Leave a comment