Ken 1 ماه پیش
والد
کامیت
f364a02b55
1فایلهای تغییر یافته به همراه40 افزوده شده و 16 حذف شده
  1. 40 16
      app/Http/Controllers/Api/Test.php

+ 40 - 16
app/Http/Controllers/Api/Test.php

@@ -3,33 +3,57 @@
 namespace App\Http\Controllers\Api;
 
 use App\Http\Controllers\Controller;
-use http\Client;
-use http\Client\Request;
-use http\QueryString;
+
 
 class Test extends Controller
 {
     public function index()
     {
-        $this->football();
-        return $this->success('你哈');
+        $data = $this->football();
+        return $this->success($data);
     }
 
     function football()
     {
-        $client = new Client();
-        $request = new Request();
-        $request->setRequestUrl('https://v3.football.api-sports.io/teams');
-        $request->setRequestMethod('GET');
-        $request->setQuery(new QueryString(['id' => '33']));
-
+        $url = 'https://v3.football.api-sports.io/teams';
+        // GET 参数
+        $params = [
+            'id' => 33
+        ];
+        // 设置 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: your-api-key",
+            "Accept: application/json",
+            "User-Agent: PHP"
+        ]);
+
+// 使用 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;
+        }
+
+// 关闭 cURL 会话
+        curl_close($ch);
+
+// 处理响应
+        return json_decode($response, true);
 
 
-        $key = env("API_FOOTBALL_KEY",'');
-        $request->setHeaders(['x-apisports-key' => $key]);
-        $client->enqueue($request)->send();
-        $response = $client->getResponse();
-        echo $response->getBody();
     }