if(!$_GET['username']||!$_GET['password']||!$_GET['domain']){ | |
exit(urldecode(json_encode(array('msg'=>'username,password,domain 参数必选')))); | |
} |
<?php | |
error_reporting(0); | |
set_time_limit(0); | |
header('Content-type:text/html; charset=utf-8'); | |
if(!$_GET['username']||!$_GET['password']||!$_GET['domain']){ | |
exit(json_encode(array('msg'=>'username,password,domain 参数必选'))); | |
} | |
$domain = explode("@", $_GET['domain']); | |
$config = array( | |
'login_email' => $_GET['username'], | |
'login_password' => $_GET['password'], | |
'sub_domain' => $domain[0], | |
'domain' => $domain[1], | |
'record_line' => $_GET['line']?$_GET['line']:'默认', | |
'ttl' => 600, | |
'format' => 'json', | |
'lang' => 'cn', | |
'error_on_empty' => 'no', | |
); | |
$dnspod = new dnspod($config); | |
$config['ip'] = $_GET['myip']?$_GET['myip']:$_SERVER['REMOTE_ADDR']; | |
$dnspod->updateRecordIp($config['ip']); | |
class dnspod { | |
var $config; | |
var $domain; | |
var $sub_domain; | |
var $record_line; | |
function __construct($config) | |
{ | |
$this->config = $config; | |
$this->domain = $config['domain']; | |
$this->sub_domain = $config['sub_domain']; | |
$this->record_line = $config['record_line']; | |
} | |
public function api_call($api, $data) { | |
if ($api == '' || !is_array($data)) { | |
exit(json_encode(array('msg'=>'内部错误:参数错误'))); | |
} | |
$api = 'https://dnsapi.cn/' . $api; | |
$data = array_merge($data,$this->config); | |
$result = $this->post_data($api, $data); | |
if (!$result) { | |
exit(json_encode(array('msg'=>'内部错误:调用失败'))); | |
} | |
$results = @json_decode($result, 1); | |
if (!is_array($results)) { | |
exit(json_encode(array('msg'=>'内部错误:返回错误'))); | |
} | |
if ($results['status']['code'] != 1) { | |
exit(json_encode(array('msg'=>$results['status']['message']))); | |
} | |
return $results; | |
} | |
public function updateRecordDdns(){ | |
$record = $this->getRecordInfo(); | |
//判断IP是否改变 | |
if($record['records'][0]['value'] == $_SERVER['REMOTE_ADDR']){ | |
exit(json_encode(array('msg'=>'记录不需要更新'))); | |
} | |
$response = $this->api_call('Record.Ddns', array('record_id'=>$record['records'][0]['id'],'record_line'=>$this->record_line)); | |
if($response){ | |
exit(json_encode($response)); | |
}else{ | |
exit(json_encode(array('msg'=>'更新失败'))); | |
} | |
} | |
//获取域名信息 | |
public function getDomainInfo(){ | |
$response = $this->api_call('Domain.Info', array('domain' =>$this->domain)); | |
return $response; | |
} | |
//获取记录 | |
function getRecordInfo(){ | |
$response = $this->api_call('Record.List', array('sub_domain'=>$this->sub_domain,'domain' =>$this->domain)); | |
return $response; | |
} | |
//修改A记录IP | |
function updateRecordIp($ip){ | |
$record = $this->getRecordInfo(); | |
//判断IP是否改变 | |
if($record['records'][0]['value']==$ip){ | |
exit(json_encode(array('msg'=>'记录不需要更新'))); | |
} | |
$response = $this->api_call('Record.Modify', array('record_id'=>$record['records'][0]['id'],'record_line'=>$this->record_line,'record_type'=>'A','value'=>$ip)); | |
return $response; | |
} | |
private function post_data($url, $data) { | |
if ($url == '' || !is_array($data)) { | |
return false; | |
} | |
$ch = @curl_init(); | |
if (!$ch) { | |
exit(json_encode(array('msg'=>'内部错误:服务器不支持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, 'DNSPod MYDDNS/0.1 (i@biner.me)'); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
} |
![]() |
1
anewg 2014-09-22 19:52:28 +08:00
5.4 之后 echo json_encode("中文", JSON_UNESCAPED_UNICODE);
5.4 之前 echo json_encode(urlencode("中文"));接收端 json_decode 后再 urldecode |
![]() |
4
GPU OP |
![]() |
5
iyaozhen 2014-09-22 20:38:10 +08:00
其实没关系的,接收的时候php_decode会把编码还原。
js我用jquery接收,不用任何处理data.msg直接获取就行。 一楼说的php5.4之后的JSON_UNESCAPED_UNICODE参数可以直接输出中文。 |
![]() |
6
zakokun 2014-09-22 21:44:08 +08:00 via iPad
先把中文urlencode一下,处理完以后再转回来urldecode
|
![]() |
9
bombless 2014-09-23 07:27:35 +08:00
我还以为是指读不出来unicode编码呢…
这种编码算是一种惯例了,不明白为啥要改 |
![]() |
10
tmkook 2014-09-23 09:08:50 +08:00
function jsonEncode($arr){
$json = json_encode($arr); return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $json); } 放出我的必杀技!!! |
![]() |
14
Actrace 2014-09-24 09:01:03 +08:00
一个比较容易懂的做法是先把数组内的元素都用urlencode处理成不需要转换的字符串,然后生成JSON字符串后再用urldecode来对整个JSON字符串进行解码(中文等将被还原),类似于金属提纯.
不过看了10楼的做法,感觉10楼效率上会有很大的优势,毕竟是正则替换,而且相对于上面提出的方案少了一个转换的流程. |