Test.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Sport;
  5. use App\Services\ApiFootball\Client;
  6. use App\Services\FixtureService;
  7. use Illuminate\Support\Facades\DB;
  8. class Test extends Controller
  9. {
  10. public function index()
  11. {
  12. // 5秒更新一次
  13. $list = $this->football('/odds/live');
  14. foreach ($list['response'] as $item) {
  15. $sport = Sport::where('data_id', $item['fixture']['id'])->first();
  16. $sport->odds = json_encode($item['odds']);
  17. $sport->save();
  18. }
  19. return $this->success($list);
  20. }
  21. function football($api, $params = [])
  22. {
  23. $url = config('services.api_football.host');
  24. $url .= $api;
  25. $url .= "?" . http_build_query($params);
  26. $key = config('services.api_football.key');
  27. $options = [
  28. 'http' => [
  29. 'method' => 'GET',
  30. 'header' => "x-rapidapi-key: {$key}"
  31. ]
  32. ];
  33. $context = stream_context_create($options);
  34. $response = file_get_contents($url, false, $context);
  35. if ($response === FALSE) {
  36. die('Error occurred while fetching data');
  37. }
  38. return json_decode($response, true);
  39. }
  40. }