api.class.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. //统计
  39. if($this->errcount) $this->apicount();
  40. $err = array();
  41. $err['code'] = $errcode;
  42. $err['message'] = $errmsg;
  43. $err['data'] = null;
  44. $err_json = json_encode_cn($err);
  45. echo $err_json;
  46. exit();//发生错误直接退出
  47. }
  48. //API调用统计
  49. public function apicount(){
  50. if($this->is_newday()){
  51. $this->count_newday();
  52. }else{
  53. $this->count_update();
  54. }
  55. }
  56. //API 设置来源
  57. public function setsource($source){
  58. $this->source = $source;
  59. }
  60. }
  61. ?>