api.class.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. const EMPTY = 201;
  15. const EMPTY_MSG= "股票代码不存在/错误";
  16. const STOCK = 202;
  17. const STOCK_MSG= "股票数据为空";
  18. //API编号,数字
  19. public $code;
  20. //API来源
  21. public $source = 0;
  22. //API 某一天,时间戳
  23. public $day;
  24. //发生错误
  25. public $error = false;
  26. //是否统计错误调用
  27. public $errcount = false;
  28. public function __construct($code, $errcount = true) {
  29. $this->code = $code;
  30. $this->day = strtotime(date('Y-m-d'));
  31. if($errcount) $this->errcount = true;
  32. }
  33. //检查IP白名单
  34. public function checkIP(){
  35. global $Array_API_IP_WhiteList;
  36. $ip = getIP();//This Function in lib/function_common@WiiPHP
  37. if(!in_array($ip, $Array_API_IP_WhiteList)) $this->ApiError('999', 'IP不在白名单');
  38. }
  39. //发生错误
  40. public function ApiError($errcode, $errmsg){
  41. $this->error = true;
  42. $err = array();
  43. $err['code'] = $errcode;
  44. $err['message'] = $errmsg;
  45. $err['data'] = null;
  46. $err_json = json_encode_cn($err);
  47. echo $err_json;
  48. exit();//发生错误直接退出
  49. }
  50. //API 设置来源
  51. public function setsource($source){
  52. $this->source = $source;
  53. }
  54. }
  55. ?>