API文档 php版api文档 站点api 获取,删除,增加,修改域名列表php案例
站点api 获取,删除,增加,修改域名列表php案例

更新时间:2020-05-09 13:36:04

文档内容
            <?php
                    $uid = 28;
                    $vhost = 'asdfw';

                $token = getToken($uid, $vhost);
                print_r($token);
                //获取token
                function getToken($uid, $vhost){
                   $skey = 'wEBxca5X9NArc5Hq';
                   $time = time();
                   $vdata = array(
                           'uid'=>$uid,
                           't'=>$time,
                           'skey'=>$skey,
                           'sign'=>md5(md5($uid.$skey).$time),
                           'vhost'=>$vhost
                   );

                   $url = 'http://192.168.1.210/api2/site/index.php/token'; //sfvft3jdsutl7sebsa0d95padn
                   return curlCall($url, $vdata)['token'];
                }
    // $domainlist = getDomainList($uid, $vhost, $token);
            // print_r($domainlist);
            //获取域名列表
            function getDomainList($uid, $vhost, $token){
               $vdata = array(
                       'uid' => $uid,
                       'vhost'=>$vhost,
                       'token'=>trim($token)
               );
               $url = 'http://192.168.1.210/api2/site/index.php/domain/list';
               return curlCall($url, $vdata);
            }
            $domainlist = addNewDomain($uid, $vhost, $token, 'test.com', '4.3.2.1');
            print_r($domainlist);
            //添加新域名
            function addNewDomain($uid, $vhost, $token, $domain, $host){
               $vdata = array(
                       'uid' => $uid,
                       'vhost'=>$vhost,
                       'token'=>trim($token),
                       'domain'=>$domain,
                       'host'=>$host
               );
          $url = 'http://192.168.1.210/api2/site/index.php/domain';
               return curlCall($url, $vdata);
            }
            // $ret = delDomainById($uid, $vhost, $token,1);
            // print_r($ret);
            //删除域名
            function delDomainById($uid, $vhost, $token, $id){
               $vdata = array(
                       'uid' => $uid,
                       'vhost'=>$vhost,
                       'token'=>trim($token)
               );
               $url = 'http://192.168.1.210/api2/site/index.php/domain/'.$id;
               return curlCall($url, $vdata, "DELETE");
            }
            $ret = editDomain($uid, $vhost, $token, "b.com", "3.3.3.3");
            print_r($ret);
            //修改域名
            function editDomain($uid, $vhost, $token, $domain, $host){
               $vdata = array(
                       'uid' => $uid,
                       'vhost'=>$vhost,
                       'token'=>trim($token),
                       'domain'=>$domain,
                       'host'=>$host
               );
               $url = 'http://192.168.1.210/api2/site/index.php/domain/2';
               return curlCall($url, $vdata, "PUT");
            }
            function curlCall(String $url, array $vdata, String $method="post"){
               $ch = curl_init();
               curl_setopt($ch,CURLOPT_URL,$url);
               curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
               curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
               curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
               $param = array();
               foreach ($vdata as $key=>$val) {
                   $arr = array_push($param, $key."=".$val);
               }
               $param = join("&",$param);//拼接后的参数原始字符串示例:uid=28&vhost=asdfw&token=o9b1fuccgurb7l6m5benjopase&domain=b.com&host=2.2.2.2
               switch(strtolower($method)) {
                   case 'get':
                       curl_setopt($ch, CURLOPT_POSTFIELDS, $param); //设置请求体,提交数据包
                       break;
                   case 'post':
                       curl_setopt($ch, CURLOPT_POST, true);
                       curl_setopt($ch, CURLOPT_POSTFIELDS, $param); //设置请求体,提交数据包
                       break;
                   case 'put':
                       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
                       curl_setopt($ch, CURLOPT_POSTFIELDS, $param); //设置请求体,提交数据包
                       break;
                   case 'delete':
                       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                       curl_setopt($ch, CURLOPT_POSTFIELDS, $param); //设置请求体,提交数据包
                       break;
               }
               curl_setopt($ch,CURLOPT_HEADER,0);
               $output = curl_exec($ch);
               if($output === FALSE ){
                   echo "CURL Error:".curl_error($ch);
               }
               curl_close($ch);
               return json_decode($output, true);
            }