common.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. use think\Env;
  3. /**
  4. * 获取响应数据
  5. *
  6. * @param string $url 请求地址
  7. * @param mixed $param 请求参数
  8. * @param string $method 请求方式
  9. * @param array $httpHeader 需要更改的表头协议
  10. * @return string
  11. */
  12. function get_http_response($url,$param,$method='post',$httpHeader='') {
  13. $ch = curl_init();
  14. if ( 'get' == $method ) {
  15. curl_setopt($ch, CURLOPT_URL, "{$url}?{$param}");
  16. } else {
  17. curl_setopt($ch, CURLOPT_URL, $url);
  18. curl_setopt($ch, CURLOPT_POST, 1);
  19. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  20. }
  21. if (strstr($url, 'https')){
  22. //https必须要跳过证书检查才能正常请求
  23. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //这个是重点,规避ssl的证书检查。
  24. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 跳过host验证
  25. }
  26. if (!empty($httpHeader)){
  27. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
  28. }
  29. curl_setopt($ch, CURLOPT_HEADER, false);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  31. // 超时设置
  32. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  33. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  34. $responseText = curl_exec($ch);
  35. curl_close($ch);
  36. $logMsg = '请求接口连接:' . $url;
  37. $logMsg .= ', 请求参数:' . $param;
  38. $logMsg .= ', 响应结果:' . $responseText;
  39. // writeLogToElk($logMsg);
  40. // log_message($logMsg,'log',LOG_PATH . 'complexLoginlog/');
  41. trace('请求: ' . $logMsg, 'complex/common@get_http_response.result');
  42. return trim($responseText);
  43. }
  44. // 请求次数控制
  45. function can_do_request($key,$times=10,$expire=3600) {
  46. if ( empty($key) ) {
  47. return '';
  48. }
  49. $times = is_numeric($times) ? (int)$times : 10;
  50. $expire = is_numeric($expire) ? (int)$expire : 24*60*60;
  51. //$redis = initAndGetRedis();
  52. $redis = new \Redis();
  53. $redis->connect(Env::get('redis.host', '127.0.0.1'), Env::get('redis.port', 6379));
  54. if (Env::get('redis.password', '')) {
  55. $redis->auth(Env::get('redis.password', ''));
  56. }
  57. $redis->select(Env::get('redis.select', 0));
  58. if ( $redis->exists($key) ) {
  59. $count = $redis->get($key);
  60. if ( $count > $times ) {
  61. return 'over request times';
  62. } else {
  63. $redis->incr($key);
  64. }
  65. } else {
  66. $redis->setex($key, $expire, 1);
  67. }
  68. return '';
  69. }
  70. /**
  71. * 获取游戏指定的用户名连接符
  72. *
  73. * @param int $gameid 游戏ID
  74. * @return string
  75. */
  76. function get_username_connector($gameid) {
  77. $specify = ['116','173','201'];
  78. if ( in_array($gameid, $specify) ) {
  79. return '#';
  80. } else {
  81. return '___';
  82. }
  83. }