| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- class Test extends Controller
- {
- public function index()
- {
- $data = $this->football();
- return $this->success($data);
- }
- function football()
- {
- $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);
- }
- }
|