Test.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. $list = $this->football('/odds/live');
  13. foreach ($list['response'] as $item) {
  14. $sport = Sport::where('data_id', $item['fixture']['id'])->first();
  15. $sport->odds = json_encode($item['odds']);
  16. $sport->save();
  17. }
  18. }
  19. function football($api, $params = [])
  20. {
  21. $url = config('services.api_football.host');
  22. $url .= $api;
  23. $url .= "?" . http_build_query($params);
  24. $key = config('services.api_football.key');
  25. $options = [
  26. 'http' => [
  27. 'method' => 'GET',
  28. 'header' => "x-rapidapi-key: {$key}"
  29. ]
  30. ];
  31. $context = stream_context_create($options);
  32. $response = file_get_contents($url, false, $context);
  33. if ($response === FALSE) {
  34. die('Error occurred while fetching data');
  35. }
  36. return json_decode($response, true);
  37. }
  38. }