AppNode 控制中心和受控端类库 PHP SDK

虚竹 2018-3-7 995

控制中心 PHP 类库


文件名:ccenter.class.php

<?php
class CCenter {
    public $Gateway='';
    public $SignKey='';
    
    public function __construct($gateway, $signkey) {
        $this->Gateway = $gateway;
        $this->SignKey = $signkey;
    }

    private function _curl($httpmethod, $params) {
        $ch = curl_init();

        switch (strtolower($httpmethod)) {
        case "get":
            $url = $this->Gateway . '/?' . http_build_query($params);
            curl_setopt($ch, CURLOPT_URL, $url);
        case "post":
            curl_setopt($ch, CURLOPT_URL, $this->Gateway);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        $rs = curl_exec($ch);
        curl_close($ch);
        
        return $rs;
    }
    
    private function _sign($params) {
        ksort($params);
        $paramstrings = array();
        foreach ($params as $key => $val) {
            $paramstrings[] = urlencode($key) . '=' . urlencode($val);
        }
        
        $strtosign = implode('&', $paramstrings);
        return hash_hmac('md5', $strtosign, $this->SignKey);
    }

    private function _randstr($len) {
        $cs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $cslen = strlen($cs);
        $s = '';
        for ($i = 0; $i != $len; $i++) {
            $x = rand(0, $cslen-1);
            $s .= $cs{$x};
        }
        return $s;
    }
    
    private function _request($httpmethod, $apptype, $app, $action, $params) {
        $globalparams = array(
            'api_action' => $action,
            'api_format' => 'json',
            'api_timestamp' => time(),
            'api_nonce' => $this->_randstr(16),
        );

        if ($apptype == "agent") {
            $globalparams['api_agent_app'] = $app;
        } else {
            $globalparams['api_ccenter_app'] = $app;
        }

        if (!$params) {
            $params = $globalparams;
        } else {
            $params = array_merge($params, $globalparams);
        }
        $params['api_sign'] = $this->_sign($params);
        $rs = $this->_curl($httpmethod, $params);
        return $rs;
    }
            
    public function Get($app, $action, $params) {
        $response = $this->_request("get", "ccenter", $app, $action, $params);
        return json_decode($response, true);
    }
            
    public function Post($app, $action, $params) {
        $response = $this->_request("post", "ccenter", $app, $action, $params);
        return json_decode($response, true);
    }
            
    public function GetAgent($nodeid, $app, $action, $params) {
        $params['api_nodeid'] = $nodeid;
        $response = $this->_request("get", "agent", $app, $action, $params);
        return json_decode($response, true);
    }
            
    public function PostAgent($nodeid, $app, $action, $params) {
        $params['api_nodeid'] = $nodeid;
        return $this->_request("post", "agent", $app, $action, $params);
    }
}

受控端 PHP 类库


文件名:agent.class.php

<?php
class Agent {
    public $Gateway='';
    public $SignKey='';

    public function __construct($gateway, $signkey) {
        $this->Gateway = $gateway;
        $this->SignKey = $signkey;
    }


    private function _curl($httpmethod, $params, $saveto) {
        $ch = curl_init();

        switch (strtolower($httpmethod)) {
        case "get":
            $url = $this->Gateway . '/?' . http_build_query($params);
            curl_setopt($ch, CURLOPT_URL, $url);
        case "post":
            curl_setopt($ch, CURLOPT_URL, $this->Gateway);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        if ($saveto) {
            $fp = fopen ($saveto, 'w+');
            curl_setopt($ch, CURLOPT_FILE, $fp);
            $rs = curl_exec($ch);
            curl_close($ch);
            fclose($fp);
        } else {
            $rs = curl_exec($ch);
            curl_close($ch);
        }
        
        return $rs;
    }

    private function _sign($params) {
        ksort($params);
        $paramstrings = array();
        foreach ($params as $key => $val) {
            $paramstrings[] = urlencode($key) . '=' . urlencode($val);
        }
        
        $strtosign = implode('&', $paramstrings);
        return hash_hmac('md5', $strtosign, $this->SignKey);
    }

    private function _randstr($len) {
        $cs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $cslen = strlen($cs);
        $s = '';
        for ($i = 0; $i != $len; $i++) {
            $x = rand(0, $cslen-1);
            $s .= $cs{$x};
        }
        return $s;
    }

    private function _request($httpmethod, $app, $action, $params, $saveto) {
        $globalparams = array(
            'api_agent_app' => $app,
            'api_action' => $action,
            'api_format' => 'json',
            'api_timestamp' => time(),
            'api_nonce' => $this->_randstr(16),
        );

        if (!$params) {
            $params = $globalparams;
        } else {
            $params = array_merge($params, $globalparams);
        }
        $params['api_sign'] = $this->_sign($params);
        $rs = $this->_curl($httpmethod, $params, $saveto);
        return $rs;
    }
            
    public function Get($app, $action, $params) {
        $response = $this->_request("get", $app, $action, $params, "");
        return json_decode($response, true);
    }
            
    public function Post($app, $action, $params) {
        $response = $this->_request("post", $app, $action, $params, "");
        return json_decode($response, true);
    }
            
    public function Download($app, $action, $params, $saveto) {
        $response = $this->_request("post", $app, $action, $params, $saveto);
        return json_decode($response, true);
    }
}
最新回复 (1)
  • 虚竹 2018-3-7
    引用 2

    控制中心 API 调用示例

    <?php
    /* 
     * AppNode控制中心API调用示例
     * 控制中心的API信息,请在面板右上角“设置”-“API设置”中获取
     */
    require "ccenter.class.php";
    
    // http://192.168.1.128:8899 为控制中心API网关地址
    // rrQdM84Iiv2FGYykanaVH6ReuNGfzOIx 为控制中心API密钥
    $ccenter = new CCenter("http://192.168.1.128:8899", "rrQdM84Iiv2FGYykanaVH6ReuNGfzOIx");
    
    header("Content-Type: text/plain");
    
    // 通过Get调用控制中心应用的API
    $result = $ccenter->Get("nodemgr", "Node.List", null);
    print_r($result);
    
    // 通过GetAgent调用指定节点的受控端应用的API
    // 1为节点ID
    // sysinfo 为【系统信息】这个应用的代号
    // Status.Overview 为接口名称
    // 应用代号请参考手册
    $result = $ccenter->GetAgent(1, "sysinfo", "Status.Overview", null);
    print_r($result);


    受控端 API 调用示例

    <?php
    /* 
     * AppNode受控端API调用示例
     * 受控端的API信息,请在“节点管理”-“节点设置”-“配置信息”中获取
     */
    require "agent.class.php";
    
    // http://192.168.1.128:9999 为受控端API网关地址
    // 95bAzsK4AuYbrEnFjfUGdku5CXz2yKJn 为受控端API密钥
    $agent = new Agent("http://192.168.1.128:9999", "95bAzsK4AuYbrEnFjfUGdku5CXz2yKJn");
    
    header("Content-Type: text/plain");
    
    // sysinfo 为【系统信息】这个应用的代号
    // Status.Overview 为接口名称
    // 应用代号请参考手册
    $result = $agent->Get("sysinfo", "Status.Overview", null);
    print_r($result);
返回
发新帖