Get started with PHP cURL and Atomia DNS API
A few days back we published a blog post on how to get started with C# and Atomia DNS. Today, we will do a similar blog post for the very popular script language PHP.
When we released Atomia DNS playgound (AtomiaDNS.net) we needed a sign-up form. Since the Atomia DNS API exposes a method to create new accounts, Marko, one of our web gurus, could do this with a simple PHP script.
To talk to the API, we used PHP cURL and with just some small changes this script can be modified to call any method exposed by the API.
Now, let’s have a look at the code!
Since we perform an administrator task (add account) we must use the admin credentials. These were set in /etc/atomiadns.conf
during the installation of Atomia DNS. When managing your zones you will replace these credentials with your own.
We also need to set the URL to the method we want to call, in this case http://api.atomiadns.net/pretty/atomiadns.json/AddAccount
. The “pretty” part of the URL is optional; including it will return a nicely formatted Json result.
In CURLOPT_POSTFIELDS
we will add the Json object with data to process by the API.
[sourcecode language=”php”]
$ch = curl_init();
$ch_username = ‘auth_admin_username’;
$ch_password = ‘auth_admin_password’;
$header = array("X-Auth-Username: $ch_username","X-Auth-Password: $ch_password");
curl_setopt($ch, CURLOPT_URL, "http://api.atomiadns.net/pretty/atomiadns.json/AddAccount");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS,"["$email","$password"]");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
[/sourcecode]
There you have it! As simple as that.
Check out http://atomiadns.com for more information about Atomia DNS and try our fully functional DNS playground at http://atomiadns.net.