SportClientService.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Http;
  4. class SportClientService
  5. {
  6. //
  7. public static function get($endpoint, $params = [])
  8. {
  9. $response = Http::withHeaders([
  10. 'x-apisports-key' => env('API_FOOTBALL_KEY'),
  11. ])
  12. ->withoutVerifying() // 临时跳过 SSL 验证
  13. ->get(env('API_FOOTBALL_HOST') . '/' . $endpoint, $params);
  14. if ($response->successful()) {
  15. return $response->json();
  16. }
  17. // Handle errors as needed
  18. throw new \Exception("API request failed: " . $response->body());
  19. }
  20. public static function post($endpoint, $data = [])
  21. {
  22. $response = Http::withHeaders([
  23. 'x-apisports-key' => config('services.api_football.key'),
  24. ])->post(config('services.api_football.host') . '/' . $endpoint, $data);
  25. if ($response->successful()) {
  26. return $response->json();
  27. }
  28. // Handle errors as needed
  29. throw new \Exception("API request failed: " . $response->body());
  30. }
  31. // 时区
  32. public static function timezone()
  33. {
  34. return self::get('timezone');
  35. }
  36. // 国家/地区
  37. public static function countries($params = [])
  38. {
  39. return self::get('countries', $params);
  40. }
  41. // 联赛 获取可用的联赛和杯赛名单。
  42. public static function leagues($params = [])
  43. {
  44. return static::get('leagues', $params);
  45. }
  46. // 联赛赛季 获取特定联赛的赛季列表。
  47. public static function leaguesSeasons($params = [])
  48. {
  49. return static::get('leagues/seasons', $params);
  50. }
  51. // This endpoint returns in-play odds for fixtures in progress.
  52. // 此端点会返回正在进行的比赛的实时赔率。
  53. // Update Frequency : This endpoint is updated every 5 seconds.
  54. // 更新频率:此端点每 5 秒钟更新一次。
  55. public static function oddsLive($params = [])
  56. {
  57. return static::get('odds/live', $params);
  58. }
  59. public static function odds($params = [])
  60. {
  61. return static::get('odds', $params);
  62. }
  63. public static function fixturesRounds($params = [])
  64. {
  65. return static::get('fixtures/rounds', $params);
  66. }
  67. // 赛程
  68. public static function fixtures($params = [])
  69. {
  70. return self::get('fixtures', $params);
  71. }
  72. }