SportClientService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Http;
  4. class SportClientService
  5. {
  6. //
  7. public static function get($type, $endpoint, $params = [])
  8. {
  9. if ($type == 1) {
  10. $host = env('API_FOOTBALL_HOST');
  11. $key = env('API_FOOTBALL_KEY');
  12. } elseif ($type == 2) {
  13. $host = env('API_BASKETBALL_HOST');
  14. $key = env('API_BASKETBALL_KEY');
  15. } else {
  16. throw new \Exception("Invalid sport type");
  17. }
  18. $response = Http::withHeaders([
  19. 'x-apisports-key' => $key,
  20. ])
  21. ->withoutVerifying() // 临时跳过 SSL 验证
  22. ->get($host . '/' . $endpoint, $params);
  23. if ($response->successful()) {
  24. return $response->json();
  25. }
  26. // Handle errors as needed
  27. throw new \Exception("API request failed: " . $response->body());
  28. }
  29. public static function post($type, $endpoint, $data = [])
  30. {
  31. if ($type == 1) {
  32. $host = env('API_FOOTBALL_HOST');
  33. $key = env('API_FOOTBALL_KEY');
  34. } elseif ($type == 2) {
  35. $host = env('API_BASKETBALL_HOST');
  36. $key = env('API_BASKETBALL_KEY');
  37. } else {
  38. throw new \Exception("Invalid sport type");
  39. }
  40. $response = Http::withHeaders([
  41. 'x-apisports-key' => $key,
  42. ])->post($host . '/' . $endpoint, $data);
  43. if ($response->successful()) {
  44. return $response->json();
  45. }
  46. // Handle errors as needed
  47. throw new \Exception("API request failed: " . $response->body());
  48. }
  49. // 时区
  50. public static function timezone()
  51. {
  52. return self::get(1, 'timezone');
  53. }
  54. // 国家/地区
  55. public static function countries($params = [])
  56. {
  57. return self::get(1, 'countries', $params);
  58. }
  59. //足球 - 联赛 获取可用的联赛和杯赛名单。
  60. public static function leagues($params = [])
  61. {
  62. return static::get(1, 'leagues', $params);
  63. }
  64. //足球 - 联赛赛季 获取特定联赛的赛季列表。
  65. public static function leaguesSeasons($params = [])
  66. {
  67. return static::get(1, 'leagues/seasons', $params);
  68. }
  69. // This endpoint returns in-play odds for fixtures in progress.
  70. // 此端点会返回正在进行的比赛的实时赔率。
  71. // Update Frequency : This endpoint is updated every 5 seconds.
  72. // 足球 - 更新频率:此端点每 5 秒钟更新一次。
  73. public static function oddsLive($params = [])
  74. {
  75. return static::get(1, 'odds/live', $params);
  76. }
  77. //足球 - 赛事赔率
  78. public static function odds($params = [])
  79. {
  80. return static::get(1, 'odds', $params);
  81. }
  82. public static function fixturesRounds($params = [])
  83. {
  84. return static::get(1, 'fixtures/rounds', $params);
  85. }
  86. //足球 - 赛程
  87. public static function fixtures($params = [])
  88. {
  89. return self::get(1, 'fixtures', $params);
  90. }
  91. //足球 - 赛事统计数据
  92. public static function statistics($params = [])
  93. {
  94. return self::get(1, 'fixtures/statistics', $params);
  95. }
  96. /**********篮球**************** */
  97. //篮球 - 赛事
  98. public static function basketballGames($params = [])
  99. {
  100. return self::get(2, 'games', $params);
  101. }
  102. //篮球 - 球队统计数据
  103. public static function basketballStatisticsTeams($params = [])
  104. {
  105. return self::get(2, 'games/statistics/teams', $params);
  106. }
  107. //篮球 - 获取比赛或联赛的赔率
  108. public static function basketballOdds($params = [])
  109. {
  110. return self::get(2, 'odds', $params);
  111. }
  112. //篮球 - 获取可参加的联赛和杯赛列表
  113. public static function basketballLeagues($params = [])
  114. {
  115. return self::get(2, 'leagues', $params);
  116. }
  117. }