Dnspod API的PHP库

今天回家,把之前没有开发完的dnspod的php库制作出来了,也算了结已心愿。
用这个库可以轻松的开发出Dnspod的DDNS Client。
部署到了开发机上,以后再也不用到处查台式机的ip啦~~

<?php
class dnspod {
    protected $_public_params;
    public function __construct($login_email, $login_password, $lang='en', $user_id=0) {
        $this->_public_params = array(
            "login_email" => $login_email,
            "login_password" => $login_password,
            "format" => "json",
            "lang" => $lang,
            "error_on_empty" => "no",
        );
        if ($user_id) {
            $this->_public_params["user_id"] = $user_id;
        }
    }
    private function _call($strFunc, $arrKeys, $arrData) {
        $strUrl = "https://dnsapi.cn/".join(".", array_map("ucfirst", explode("_", $strFunc)));
        if (count($arrData) > count($arrKeys)) {
            return FALSE;
        }
        while (count($arrData) < count($arrKeys)) {
            array_pop($arrKeys);
        }
        $arrParam = array_merge($this->_public_params, array_combine($arrKeys, $arrData));
        $arrContext = array();
        $arrContext["http"] = array (
            "method" => "POST",
            "header" => "User-Agent: Myagent/1.0 (zhangzhenyu@baidu.com)\r\n",
            "content" => http_build_query($arrParam),
        );
        $strRes = file_get_contents($strUrl, FALSE, stream_context_create($arrContext));
        return $strRes ? json_decode($strRes, TRUE) : FALSE;
    }
    public function record_list() {
        $arrKey = explode(",", "domain_id,offset,length");
        $arrVal = func_get_args();
        return $this->_call(__FUNCTION__, $arrKey, $arrVal);
    }
    public function domain_list() {
        $arrKey = explode(",", "type,offset,length,group");
        $arrVal = func_get_args();
        return $this->_call(__FUNCTION__, $arrKey, $arrVal);
    }
    public function record_status($domain_id, $record_id, $status=NULL) {
        $arrKey = explode(",", "domain_id,record_id,status");
        $arrVal = func_get_args();
        return $this->_call(__FUNCTION__, $arrKey, $arrVal);
    }
    public function record_modify() {
        $arrKey = explode(",", "domain_id,record_id,sub_domain,record_type,record_line,value,mx,ttl");
        $arrVal = func_get_args();
        return $this->_call(__FUNCTION__, $arrKey, $arrVal);
    }
}
$objDns = new dnspod("用户名", "密码");
print_r($objDns->domain_list());
//print_r($objDns->record_list(123));
//print_r($objDns->record_status(123, 456, "enable"));
//print_r($objDns->record_modify(123, 456, "www", "A", "默认", "119.75.218.45"));

大部分函数都没有填充,我只填充了几个我需要用到的函数。
>>API文档<<
按照sample和api文档填充进去就可以使用

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>