树莓派建立php网页,树莓派实现DDNS(PHP计划任务)

780aad5e2eb41d4016a316c6c8d71dfc.png

之前介绍了DNSPod动态IP解析更新程序,是Python实现的。下面介绍一个PHP实现的脚本,实现了同样的动态域名解析功能。

<?php header("Content-type: text/html; charset=utf8"); class Dns { #Dnspod账户 private $dnspod_user = 'user@example.com'; #Dnspod密码 private $dnspod_pwd = 'password'; #Dnspod主域名,注意:是你注册的域名 private $domain = 'example.com'; #子域名,如www,如果要使用根域名,用@ private $sub_domain = 'www'; function getMyIp() { try { $ip = file_get_contents('http://www.leadnt.com/tools/ip.php'); return $ip; } catch(Exception $e) { echo $e->getMessage();

return null;

}

}

function api_call($api, $data)

{

if ($api == '' || !is_array($data)) {

exit('内部错误:参数错误');

}

$api = 'https://dnsapi.cn/' . $api;

$data = array_merge($data, array('login_email' => $this->dnspod_user, 'login_password' => $this->dnspod_pwd, 'format' => 'json', 'lang' => 'cn', 'error_on_empty' => 'no'));

$result = $this->post_data($api, $data);

if (!$result) {

exit('内部错误:调用失败');

}

$results = @json_decode($result, 1);

if (!is_array($results)) {

exit('内部错误:返回错误');

}

if ($results['status']['code'] != 1) {

exit($results['status']['message']);

}

return $results;

}

private function post_data($url, $data)

{

if ($url == '' || !is_array($data)) {

return false;

}

$ch = @curl_init();

if (!$ch) {

exit('内部错误:服务器不支持CURL');

}

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

curl_setopt($ch, CURLOPT_USERAGENT, 'LocalDomains_PHP/1.0.0(roy@leadnt.com)');

$result = curl_exec($ch);

curl_close($ch);

return $result;

}

public function exec()

{

$ip = $this->getMyIp();

$domainInfo = $this->api_call('domain.info',array('domain' => $this->domain));

$domainId = $domainInfo['domain']['id'];

$record = $this->api_call('record.list',array('domain_id'=> $domainId,'offset' =>'0','length' => '1','sub_domain' =>$this->sub_domain));

if($record['info']['record_total'] == 0)

{

$this->api_call('record.create',

array(

'domain_id' => $domainId,

'sub_domain' => $this->sub_domain,

'record_type' => 'A',

'record_line' => '默认',

'value' => $ip,

'ttl' => '3600'

));

}

else

{

if($record['records'][0]['value'] != $ip)

{

$this->api_call('record.modify',

array(

'domain_id' => $domainId,

'record_id' => $record['records'][0]['id'],

'sub_domain' => $this->sub_domain,

'record_type' => 'A',

'record_line' => '默认',

'value' => $ip

));

}

else

{

echo '指向正常';

}

}

}

}

$dns = new Dns();

$dns->exec();

?>

sudo chmod 777 dnspod.php

加入到定时任务,每个小时执行一次。

vi /etc/crontab

*/55 * * * * root /usr/bin/php /home/pi/dnspod.php >> /home/pi/dnspod.log 2>&1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值