Client.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Services\ApiFootball;
  3. use Illuminate\Support\Facades\Http;
  4. class Client
  5. {
  6. //
  7. public static function get($endpoint, $params = [])
  8. {
  9. $response = Http::withHeaders([
  10. 'x-apisports-key' => config('services.api_football.key'),
  11. ])
  12. ->withoutVerifying() // 临时跳过 SSL 验证
  13. ->get(config('services.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. public static function leagues($params = [])
  42. {
  43. return static::get('leagues', $params);
  44. }
  45. // 赛程
  46. public static function fixtures($params = [])
  47. {
  48. return self::get('fixtures', $params);
  49. }
  50. }