WHMCS 自动登录接口
使用方法:将如下代码保存名为 ssologin.php 的文件,放入 WHMCS 根目录
<?php
require "init.php";
// 获取用户的 Cookie 字符串
$cookie = $_SERVER['HTTP_COOKIE'] ?? '';
// 使用正则表达式提取包含 "WHMCS" 后面有随机内容的 token 值
$pattern = '/WHMCS.*?=(\w+)/';
preg_match($pattern, $cookie, $matches);
// 若未找到 token
if (empty($matches[1])) {
// 设置 HTTP 状态码为 403
http_response_code(403);
// 返回错误消息
$err = [
"result" => "error",
"message" => "未获取到 Token",
];
// 输出 JSON 格式的错误消息
echo json_encode($err);
exit;
}
// $token = $matches[1];
$clientId = WHMCS\Session::get("uid");
// 从查询字符串中获取 destination 参数
$destination = $_GET['destination'] ?? 'clientarea:product_details';
$command = 'CreateSsoToken';
$postData = array(
'client_id' => $clientId,
'destination' => $destination,
);
$adminUsername = 'vortex'; // 填写管理员用户名 Optional for WHMCS 7.2 and later
$results = localAPI($command, $postData, $adminUsername);
header('Content-Type: application/json');
// 构建要返回的数据
$responseData = ['data' => $results];
echo(json_encode($responseData));
?>Last updated