使用php sdk编写服务端

使用php的sdk可以大大简化api json的编写, 代码如下:

输出api json

//定义api
$provider = new prism_provider("/real/api/router");

$provider->add("get_id_by_domain", 
        prism_api("id",
            prism_params("arg1", "useinput"),
            prism_params("arg2", "useinput"),
            prism_params("arg3", "useinput"),
            prism_params("arg4", "useinput")
        )
    );

输出api的json格式
$provider->output_json();

如此即可输出api的配置json. 适用于已有许多接口, 仅希望用JSON输出结构.

{
   "url":"\/prism_sdk\/examples\/server.php",
   "interface":"",
   "sandbox_url":"",
   "resource_content_types":"text\/json",
   "prefix":"",
   "summary":"",
   "apis":{
      "get_id_by_domain":{
         "path":"",
         "method":[
            "POST"
         ],
         "summary":"",
         "notes":"",
         "require_oauth":"",
         "backend_timeout_second":"",
         "params":[
            {
               "name":"arg1",
               "desc":"useinput",
               "required":false,
               "type":"string",
               "param_type":"request"
            },
            {
               "name":"arg2",
               "desc":"useinput",
               "required":false,
               "type":"string",
               "param_type":"request"
            },
            {
               "name":"arg3",
               "desc":"useinput",
               "required":false,
               "type":"string",
               "param_type":"request"
            },
            {
               "name":"arg4",
               "desc":"useinput",
               "required":false,
               "type":"string",
               "param_type":"request"
            }
         ],
         "response":"",
         "exception":[

         ]
      }
   },
   "mode":"params",
   "param_name":"method",
   "models":[

   ],
   "auto_config_url":"",
   "config_values":[

   ],
   "global_params":[

   ]
}

处理api调用

如果在定义api的时候定义相应的handle方法, 则可以进行api的调度以及签名, 错误处理等操作.

class my_api_handler{

    public function get_id_by_domain($params){
        return $_SERVER;
    }

}

$provider->handler(new my_api_handler);

if(array_key_exists('show_api_json', $_GET)){
    $provider->output_json();
}elseif(array_key_exists("method", $_REQUEST)){
    $provider->dispatch($_REQUEST["method"]);
}

设置签名验证方式

$provider->set_validation("prism_sign_validation", "get_secret_by_key");

set_validation(prism_validation, construct args...)

其中, 用于验证的类需要符合接口prism_validation

interface prism_validation{
    function get_config();
    function get_global_params();
    function validate();
}

prism_sign_validation是sdk自带. 采用key与secret的组合进行签名, 需要设置一个回调函数用来验证key与secret的正确性.


沪ICP备05002918号

© 2003-2017 ShopEx,Inc.All rights reserved.