Test.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\ApiFootball\Client;
  5. use App\Services\FixtureService;
  6. use Illuminate\Support\Facades\DB;
  7. class Test extends Controller
  8. {
  9. public function index()
  10. {
  11. // $data = $this->football('/countries',[]);
  12. // $data = $this->football('/leagues',['code'=>'DZ']);
  13. // $data = $this->football('/fixtures', ['date' => '2026-03-05']);
  14. // $data[] = Client::timezone();
  15. // $data[] = $this->football('/fixtures', ['date' => '2026-03-05']);
  16. // $data[] = $this->football('/odds',['fixture'=>'1491915']);
  17. // $data = FixtureService::index();
  18. $id = request()->input('id', '1492541');
  19. $sport = DB::select("SELECT * FROM sports WHERE data_id = {$id}");
  20. $sport = $sport[0];
  21. $list = $this->football('/odds/live',['fixture'=>$id]);
  22. $data[] = $sport;
  23. foreach ($list['response'] as $item) {
  24. if ($item['fixture']['id'] == $sport->data_id) {
  25. $data[] = $item;
  26. break;
  27. }
  28. }
  29. $data[] = $list;
  30. return $this->success($data);
  31. }
  32. function football($api, $params = [])
  33. {
  34. $url = config('services.api_football.host');
  35. $url .= $api;
  36. $url .= "?" . http_build_query($params);
  37. $key = config('services.api_football.key');
  38. $options = [
  39. 'http' => [
  40. 'method' => 'GET',
  41. 'header' => "x-rapidapi-key: {$key}"
  42. ]
  43. ];
  44. $context = stream_context_create($options);
  45. $response = file_get_contents($url, false, $context);
  46. if ($response === FALSE) {
  47. die('Error occurred while fetching data');
  48. }
  49. return json_decode($response, true);
  50. }
  51. }