Last active
August 29, 2015 14:17
-
-
Save T3chArmy/7e9cf45d45f23fb9e6ff to your computer and use it in GitHub Desktop.
CloudFlare Dynamic IP PHP Update Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# set your user, token (key), and domains | |
$CloudFlareUser='<Your CloudFlare Email>'; | |
$CloudFlareToken='<Your CloudFlare API Token>'; | |
$CloudFlareHosts=[]; # an array of the domains you want to update ex: ['example.com', 'myothersite.com'] | |
$noUpdate = []; # an array of the sub domains you don't want updated ex: ['mail.example.com'] | |
# This script will only update A records, so don't worry about ignoring CNAME or other record types | |
# the v2 of HTTP PECL extension is required for this to work correctly | |
# it can be found here: http://devel-m6w6.rhcloud.com/mdref/http | |
$req = new \http\Client\Request( | |
'GET', | |
'http://myip.dnsomatic.com/', | |
[] | |
); | |
$client = (new \http\Client()) | |
->enqueue($req) | |
->send(); | |
$serverIP = $client->getResponse()->getBody(); | |
foreach($CloudFlareHosts as $host) | |
{ | |
# build the url you need to do the update | |
$requestURL = "https://www.cloudflare.com/api_json.html?email={$CloudFlareUser}&tkn={$CloudFlareToken}&a=rec_load_all&z={$host}"; | |
$req = new \http\Client\Request( | |
'GET', | |
$requestURL, | |
[] | |
); | |
$client->reset()->enqueue($req)->send(); | |
$content = json_decode($client->getResponse()->getBody(), true); | |
foreach($content['response']['recs']['objs'] as $record) | |
{ | |
if(in_array($record['name'], $noUpdate)) | |
{ | |
echo "Record is in the no update list, ignoring the record: {$record['name']}!\n"; | |
continue; | |
} | |
if | |
{ | |
echo "Not Type 'A' Record, ignoring the record: {$record['name']}!\n"; | |
continue; | |
} | |
if($record['content'] === "$serverIP") | |
{ | |
echo "Record is already up to date, ignoring the record: {$record['name']}!\n"; | |
continue; | |
} | |
$updateURL = "https://www.cloudflare.com/api_json.html?email={$CloudFlareUser}&tkn={$CloudFlareToken}&a=rec_edit&z={$host}&name={$record['name']}&id={$record['rec_id']}&type=A&content={$serverIP}&service_mode=1&ttl=1"; | |
$req = new \http\Client\Request( | |
'GET', | |
$updateURL, | |
[] | |
); | |
$client->reset()->enqueue($req)->send(); | |
$returned = json_decode($client->getResponse()->getBody(), true); | |
if($returned['result'] === 'error') | |
{ | |
echo "Error occurred when attempting to update {$record['name']}: {$returned['msg']}; ({$returned['err_code']})\n"; | |
} else { | |
echo "{$record['name']} updated successfully!\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment