基于STM32的多功能环境感知时钟闹钟设计与实现
/** * SakuraFrp API 客户端 * 仅实现了获取隧道列表的功能 * * version
0 * author AI Assistant */ class SakuraFrpApi { /** * API 基础 URL * var string */ private const BASE_URL https://api.natfrp.com/v4; /** * 用户认证 Token * var string */ private $userToken; /** * 构造函数 * param string $userToken 您的 SakuraFrp 用户 Token */ public function __construct(string $userToken) { $this-userToken $userToken; } /** * 获取隧道列表 * 对应 C# 中的 tunnels() 方法 * * return array|null 成功时返回隧道列表数组失败时返回 null * throws Exception 如果请求失败或发生 cURL 错误 */ public function getTunnels(): ?array { $url self::BASE_URL . /tunnels; //echo 请求 URL: . $url . \n; return $this-makeRequest(GET, $url); } /** * 通用的请求方法 * * param string $method HTTP 方法 (GET, POST, etc.) * param string $url 请求的 URL * param array|null $data 对于 POST 请求要发送的 JSON 数据 * return array|null 成功时返回解码后的 JSON 数组失败时返回 null * throws Exception */ private function makeRequest(string $method, string $url, ?array $data null): ?array { //
初始化 cURL $ch curl_init(); //
设置 cURL 选项 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将响应作为字符串返回而不是直接输出 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 跟随重定向 // 添加 SSL 验证选项开发环境可禁用生产环境应启用 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用 SSL 证书验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不检查证书中的主机名 //
设置请求头 $headers [ Accept: application/json, Authorization: Bearer . $this-userToken ]; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //
如果是 POST 请求设置 POST 数据 if ($method POST $data ! null) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $headers[] Content-Type: application/json; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } //
执行请求 $response curl_exec($ch); //
检查错误 if (curl_errno($ch)) { $error_msg curl_error($ch); curl_close($ch); throw new Exception(cURL Error: . $error_msg); } //
获取 HTTP 状态码 $httpCode curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); //
处理响应 if ($httpCode 200 $httpCode
{ // 成功响应 $decodedResponse json_decode($response, true); if (json_last_error() JSON_ERROR_NONE) { return $decodedResponse; } throw new Exception(JSON Decode Error: . json_last_error_msg()); } else { // 错误响应 $errorResponse json_decode($response, true); $errorMessage $errorResponse[message] ?? $response; throw new Exception(API Request Failed with HTTP Code {$httpCode}: {$errorMessage}); } return null; } }
sss羞羞漫画-sss羞羞漫画应用