PHP脚本在CP​​anel中编辑DNS记录 [英] PHP Script to Edit DNS Records in CPanel

查看:88
本文介绍了PHP脚本在CP​​anel中编辑DNS记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想变得自给自足,因此没有DNSDynamic和DYNDNS之类的服务.而且我不喜欢为自己能做的服务付费.

I would like to become self-sufficient, and therefore do without services such as DNSDynamic and DYNDNS. And I don't like paying for services that I could do myself.

所以这是场景-我的主要网站托管在一家托管公司中.我也有一个带有音乐等的家庭服务器.但是问题是我的ISP(BT)没有为使用者提供静态IP地址.

So here's the scenario - I have my main website hosted with a hosting company. I also have a home server with my music, etc. on it. But the problem is that my ISP (BT) does not offer consumers Static IP addresses.

我希望主域的一个子域(指向我的主主机)指向我的家庭IP.这是用我自己完成的简单A记录完成的.

I would like to have a subdomain of my main domain (which points to my main host) point to my home IP. This is done with a simple A record - which I have done myself.

这归结为我想要制作一个PHP脚本(由我的家庭服务器上的cron作业运行)以将cPanel中的A记录更新为我的家庭服务器的当前IP.

This boils down to me wanting to make a PHP script (run by a cron job on my home server) to update the A records in cPanel to the current IP of my home server.

这里有一些示例代码-当然,缺少与cPanel进行通信的API代码,我要非常感谢能为我填补空白的任何人.

Here's some sample code - of course the bit that's missing is the API Code to communicate with cPanel, and I'd be very thankful to anyone who could fill in the gaps for me.

<?php
    $current_ip = file_get_contents("http://mydomain.com/getip.php");
    $username = "CPANEL_USERNAME";
    $password = "CPANEL_PASSWORD";
    $domain = "CPANEL_DOMAIN";

    $request = file_get_contents("http://someapipage?username=".$username."&pw=".$password."&domain=".$domain."&ip=".$current_ip);

?>

http://mydomain.com/getip.php中的代码很简单

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

由于我的家庭服务器正在运行Ubuntu,因此我已经掌握了如何设置cron作业的知识,并且已经阅读了使用wget在本地主机目录中调用cron.php的教程.

I already have the grasp of how to set up a cron job, as my home server is running Ubuntu, and I have read tutorials that call my cron.php in my localhost directory using wget.

我尝试了此链接,但我无法理解他在做什么.预先感谢.

I have tried this link but I couldn't fathom what he was doing. Thanks in advance.

推荐答案

我刚刚基于cPanel的JSON-API文档和该文档的jordih.net链接编写了该库.它没有很好的记录,但是要点是:

I have just written this library for cPanel's JSON-API based on their documentation and the jordih.net links to that documentation. It's not so well documented, but the gist of it is:

通过调用创建zone_records对象

Create a zone_records object by calling

$zones = new zone_records("cpaneluser", "pass", "website_to_login", "domain_of_records")

请注意,如果要在要更改记录的服务器上运行该网站,则登录的网站通常为127.0.0.1.

Note that the website to login is usually 127.0.0.1 if you are running this from the server you want to change the records on.

一旦被呼叫,您就可以访问成员$zones->DNSrecords.它包含一组DNS A记录和CNAME记录(两者都是DNSrecord对象).其他(TXT除外)无关紧要,因为如果不对类进行额外的添加(功能),就无法对其进行编辑.

Once called, you can then access a member $zones->DNSrecords. This contains an array of the DNS A records and CNAME records (both of which are DNSrecord objects). The others (except TXT) are irrelevant since you cannot edit them without extra additions (functions) to the classes.

每个DNSrecord都有一些成员(例如目标,ttl,名称,类型),而private则可以通过$record->ttl访问,因为我已经添加了魔术" __get方法.实现了魔术" __set方法,仅更改ttl和目标(无法使用此API函数更改其他属性,如果尝试这样做,则对象将引发异常).

Each DNSrecord has some members (e.g target, ttl, name, type) which, while private, can be accessed via $record->ttl because I have added the "magic" __get method. The "magic" __set method is implemented to change the ttl and target only (you cannot change other properties using this API function, and the object will raise an exception if you try to do so).

您可以使用

$zones->addrecord($type, $target, $name, $ttl)

添加记录,或

$zones->deleterecord($line)

要删除区域文件中$line行上的记录-您可以通过$record->line找到它.

to delete the record that is on line $line in the zone file - you can find it via $record->line.

如果要在ZoneEdit模块中进行一些查询,可以致电

If you want to make some of your own queries in the ZoneEdit module, you can call

$zones->doquery("function_from_API", array("parameters=>"here"), array("headers"=>"here"))

,它将返回cPanel响应(以及addrecord和deleterecord方法).最后,我建议您使用try {...} catch (Exception $e) {...},因为如果/当出现问题时,我的对象会引发异常(您可以当然地对其进行编辑).

and it will return the cPanel response (as will the addrecord and deleterecord methods). Finally, I would advise you to use try {...} catch (Exception $e) {...} since my objects throw exceptions if/when something goes wrong (you can edit them out of course).

此代码在公共领域-您可以在 https://github.com/ShadowLNC/cpanel_dns上获取该代码. (classdns.php是主文件,而dns_update.php显示了一个示例).

This code is in the public domain - you can get it at https://github.com/ShadowLNC/cpanel_dns (classdns.php is the main file, and dns_update.php shows an example).

这篇关于PHP脚本在CP​​anel中编辑DNS记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆