【问题标题】:Using cPanel LiveAPI PHP Class on Siteground shared hosting account在 Siteground 共享主机帐户上使用 cPanel LiveAPI PHP 类
【发布时间】:2013-09-30 08:07:07
【问题描述】:

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

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

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

php create subdomain over cPanel API

提前感谢任何帮助和我的问候。

谢谢。

【问题讨论】:

    标签: php api subdomain cpanel


    【解决方案1】:

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

    但是,将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";
    

    【讨论】:

      【解决方案2】:

      我相信您并没有在寻找 LiveAPI,因为 LiveAPI 是用于在 cPAnel/WHM 内部进行开发的。 LiveAPI 用于在 cPanel 和 WHM 接口中创建插件。

      如果您希望将子域添加到您的帐户,JSON/XML API 更适合您的任务。如果可能,请使用 JSON api,因为 cPanel Docs 将其列为首选 API,因为它比 XML api 更快。要使用 JSON/XML API 添加子域,您可以使用以下 API 调用:

      XML:

      https://domain.tld:2083/xml-api/cpanel?cpanel_xmlapi_func=addsubdomain&cpanel_xmlapi_module=SubDomain&cpanel_xmlapi_version=2&domain=sub&rootdomain=maindomain.tld

      JSON:

      https://domain.tld:2083/json-api/cpanel?cpanel_jsonapi_func=addsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=sub&rootdomain=maindomain.tld

      在上面的字符串中,您需要修改的参数是:

      • domain(字符串)- 您要添加的子域的本地部分。 (例如,如果子域是 sub.example.com,则为“sub”)此值不应包括与子域关联的域。
      • rootdomain(字符串)- 您希望添加子域的域。

      以下是进一步的文档,包括如何将这些 API 命令集成到您的 php 脚本以及如何授权 API。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-16
        • 1970-01-01
        • 1970-01-01
        • 2017-07-09
        • 2018-09-04
        • 1970-01-01
        • 2010-09-24
        • 1970-01-01
        相关资源
        最近更新 更多