Ken hace 1 mes
padre
commit
bb44865af4
Se han modificado 1 ficheros con 11 adiciones y 35 borrados
  1. 11 35
      app/Http/Controllers/Api/Test.php

+ 11 - 35
app/Http/Controllers/Api/Test.php

@@ -9,50 +9,26 @@ class Test extends Controller
 {
     public function index()
     {
-        $data = $this->football();
+       $data =  $this->football();
         return $this->success($data);
     }
 
     function football()
     {
         $url = 'https://v3.football.api-sports.io/teams';
-        // GET 参数
-        $params = [
-            'id' => 33
+        $key = env("API_FOOTBALL_KEY",'');
+        $options = [
+            'http' => [
+                'method' => 'GET',
+                'header' => "x-rapidapi-key: {$key}"
+            ]
         ];
-        // 设置 HTTP 请求的上下文
-        $key = env("API_FOOTBALL_KEY", '');
-        // 初始化 cURL 会话
-        $ch = curl_init();
-
-// 设置 cURL 选项
-        curl_setopt($ch, CURLOPT_URL, $url);  // 不用拼接查询字符串
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-        curl_setopt($ch, CURLOPT_HTTPHEADER, [
-            "x-rapidapi-key: {$key}"
-
-        ]);
-
-// 使用 POSTFIELDS 设置 GET 参数
-        curl_setopt($ch, CURLOPT_HTTPGET, true);  // 让 cURL 知道这是一个 GET 请求
-        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));  // 设置 GET 参数
-
-// 执行 cURL 请求
-        $response = curl_exec($ch);
-
-// 检查请求是否成功
-        if (curl_errno($ch)) {
-            echo 'cURL error: ' . curl_error($ch);
-            exit;
+        $context = stream_context_create($options);
+        $response = file_get_contents($url, false, $context);
+        if ($response === FALSE) {
+            die('Error occurred while fetching data');
         }
-
-// 关闭 cURL 会话
-        curl_close($ch);
-
-// 处理响应
         return json_decode($response, true);
-
-
     }