api.class.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * api.class.php 接口类
  4. *
  5. * @version v0.02
  6. * @create time 2014/7/24
  7. * @update time 2016/5/22
  8. * @author jt
  9. * @copyright Copyright (c) 微普科技 WiiPu Tech Inc. (http://www.wiipu.com)
  10. */
  11. class API {
  12. const SUCCESS = 200;
  13. const SUCCESS_MSG= "success";
  14. //API编号,数字
  15. public $code;
  16. //API来源
  17. public $source = 0;
  18. //API 某一天,时间戳
  19. public $day;
  20. //发生错误
  21. public $error = false;
  22. //是否统计错误调用
  23. public $errcount = false;
  24. public function __construct($code, $errcount = true) {
  25. $this->code = $code;
  26. $this->day = strtotime(date('Y-m-d'));
  27. if($errcount) $this->errcount = true;
  28. }
  29. //检查IP白名单
  30. public function checkIP(){
  31. global $Array_API_IP_WhiteList;
  32. $ip = getIP();//This Function in lib/function_common@WiiPHP
  33. if(!in_array($ip, $Array_API_IP_WhiteList)) $this->ApiError('999', 'IP不在白名单');
  34. }
  35. //发生错误
  36. public function ApiError($errcode, $errmsg){
  37. $this->error = true;
  38. $err = array();
  39. $err['code'] = $errcode;
  40. $err['message'] = $errmsg;
  41. $err['data'] = null;
  42. $err_json = json_encode_cn($err);
  43. echo $err_json;
  44. exit();//发生错误直接退出
  45. }
  46. //API 设置来源
  47. public function setsource($source){
  48. $this->source = $source;
  49. }
  50. }
  51. ?>