gpt4 book ai didi

php - 在 Siteground 共享主机帐户上使用 cPanel LiveAPI PHP 类

转载 作者:可可西里 更新时间:2023-11-01 00:45:55 31 4
gpt4 key购买 nike

我想知道是否有人在共享托管服务提供商(在我的例子中是 Siteground)上实现了对 cPanel 的 PHP 应用程序级访问。我一直在查看 LiveAPI PHP 网站上的文档,它提到它涉及管理主 cPanel 安装目录中的一些文件。我找不到对任何可下载资源的引用,因此,如果可以提供指向这些资源的链接以及您如何执行实现的示例,那就太好了。

我希望以编程方式(在 PHP 中)在 cPanel 中创建子域并为其提供相应的路由目录。

我发现了这个相关问题,但它导致死胡同,因为主要的 PHP 类链接不起作用

https://stackoverflow.com/questions/7549015/php-create-subdomain-over-cpanel-api

最佳答案

Citizen Kepler 的链接现已失效,github 上的 XMLAPI here已弃用。

但是,结合给出的代码 here用于身份验证和 here为了添加子域,为我们提供了以下脚本,它似乎在共享主机上工作得很好:

<?php 

$cpanelusername = "example";
$cpanelpassword = "**********";
$subdomain = 'newsubdomain';
$domain = 'example.com';
$directory = "/public_html/$subdomain"; // A valid directory path, relative to the user's home directory. Or you can use "/$subdomain" depending on how you want to structure your directory tree for all the subdomains.

$query = "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_func=addsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=$subdomain&rootdomain=$domain&dir=$directory";

$curl = curl_init(); // Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); // Allow self-signed certs
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0); // Allow certs that do not match the hostname
curl_setopt($curl, CURLOPT_HEADER,0); // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); // Return contents of transfer on curl_exec
$header[0] = "Authorization: Basic " . base64_encode($cpanelusername.":".$cpanelpassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // set the username and password
curl_setopt($curl, CURLOPT_URL, $query); // execute the query
$result = curl_exec($curl);
if ($result == false) {
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
// log error if curl exec fails
}
curl_close($curl);

print $result;

?>

结果应该是这样的:

{"cpanelresult":{"func":"addsubdomain","event":{"result":1},"apiversion":2,"module":"SubDomain","data":[{"reason":"The subdomain “newsubdomain.example.com” has been added.","result":1}],"preevent":{"result":1},"postevent":{"result":1}}}

然后要删除子域,请运行相同的脚本,但使用此查询:

$deletesub =  "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_func=delsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=".$subdomain.'.'.$domain."&dir=$directory";  //Note: To delete the subdomain of an addon domain, separate the subdomain with an underscore (_) instead of a dot (.). For example, use the following format: subdomain_addondomain.tld

要删除目录(包括其所有内容),请运行:

$deletedir =  "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_module=Fileman&cpanel_jsonapi_func=fileop&op=unlink&sourcefiles=$directory";

关于php - 在 Siteground 共享主机帐户上使用 cPanel LiveAPI PHP 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18992945/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com