zoukankan      html  css  js  c++  java
  • ecstor openapi接口开发 增加签名验证

    1、新建一个app,如果testapi

    2、testapi下面有app.xml和service.xml,app.xml就不再说了,在service.xml注册一个service。

     <service id="openapi.testapi">
            <class>testapi_testapi</class>
        </service>

    3、testapi下面新建一个lib文件夹,在lib下面建立testapi.php

    4、// 以下代码是自己研究的,错误的地方欢迎纠正

    <?php
    
    /* 
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    class testapi_testapi{
        
        private $token;
                
        function __construct() {
            $this->token='ecstore_testopenapi'    ;
            
            
        }
        
        function sign_check($params)
        {
            $sign=$params['sign'];//远程传递过来的sign
            
            if (isset($params['sign'])) {
                unset($params['sign']);
            }
            
             $sign2= $this->get_sign($params,  $this->token);//本地sign
           
             if($sign===$sign2)
             {
                 return true;
             }
            else {
                $error['code']='0x009';
                $this->send_user_error($error);
            }
        }
    
    
        function test(){
           
           $args=$_POST;
           $this->sign_check($args);
             if($arg==1)
            {
                 $this->send_user_succ('我的测试成功');
            }else
            {
                $error['code']='0x003';
                $this->send_user_error($error);
            }
        }
        
           
         //返回成功
        function send_user_succ($result){
                $result_json = array(
                    'rsp'=>'succ',
                    'data'=>$result,
                );
               echo json_encode($result_json);
           exit;
        }
    
        //返回失败
        function send_user_error($error){
                
                 if( $error['code'] == null ){
                    $error_code = null;
                    $error_msg  =  $error['msg'];
                }else{
                    $arr_error = $this->_error_list();
                    $error_code = $error['code'];
                    $error_msg  = $arr_error[$error_code];
                }
                
                $res = array(
                    'rsp'   =>  'fail',
                    'res'   =>  $error_code,
                    'data'  =>  $error_msg,
                );
                echo json_encode($res);
            exit;
        }
            
            //获取签名验证
            function get_sign($params,$token){
                return strtoupper(md5(strtoupper(md5($this->assemble($params))).$token));
            }
            
            function assemble($params)
            {
                if(!is_array($params))  return null;
                ksort($params,SORT_STRING);
                $sign = '';
                foreach($params AS $key=>$val){
                    $sign .= $key . (is_array($val) ? assemble($val) : $val);
                }
                return $sign;
            }
            
            function _error_list(){
            $error_msg = array(
                '0x001' => '用户名或密码错误',
                '0x002' => '请求/执行超时',
                '0x003' => '数据异常',
                '0x004' => '数据库执行失败',
                '0x005' => '服务器异常',
                '0x006' => '用户权限不够',
                '0x007' => '服务不可以用',
                '0x008' => '方法不可用',
                '0x009' => '签名无效',
                '0x010' => '版本丢失',
                '0x011' => 'API 版本异常',
                '0x012' => 'API 需要升级',
                '0x013' => '网店服务异常',
                '0x014' => '网店空间不足',
                '0x020' => 'session 已过期,请重新登录',
                '0x021' => '数据冲突,本地数据不是最新数据',          
            );
            return $error_msg;
        }
    }
  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/limonyun/p/7716489.html
Copyright © 2011-2022 走看看