| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?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: {$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;
- }
- // 关闭 cURL 会话
- curl_close($ch);
- // 处理响应
- return json_decode($response, true);
- }
- }
|