php - 如何使用 cURL 创建子域?

标签 php curl subdomain cpanel

我想在我的网站上创建子域。我的托管计划不支持子域通配符条目。所以,我想通过在 PHP 中使用 cURL 来创建它。 我的问题解释如下:

此链接用于创建子域:

https://cpanel.myhost.com/cpanel/indexcp.php?option=subdomains

上面链接中的这个表单创建了一个子域:

<form method="post" action="/cpanel/indexcp.php?option=subdomains_add" name="domain">
        <table>
            <tr>
                <td align="right"><b>Subdomain</b> :&nbsp; </td>
                <td><input type="text" onchange="updatedir(this);" id="domain" name="DomainName" />&nbsp;.&nbsp;
                    <select name="domain_selector" style="width: 300px">
                        <option>mydomain.com</option>
                    </select>
                </td>
                <td><div id="domain_error" style="height: 16px; width: 16px"></div></td>
            </tr>
                    </table>
                </td>
                <td></td>
            </tr>
            <tr><td colspan="3"><br /></td></tr>
            <tr>
                <td>&nbsp;</td>
                <td><input class="input-button" id="subdomain_submit" type="submit" value="Create" name="B1" />

                </td>
                <td></td>
            </tr>
        </table>
    </form>

那么,如何使用 cURL 从我的 cPanel 创建子域?

最佳答案

此代码使用 cPanel 的 JSON 版本的“API2”,可以满足您的需要。我没有使用标准的 cPanel 帐户对其进行测试,仅使用具有 WHM 访问权限的帐户进行了测试。我希望这会有所帮助。

只需将 $subdomain$rootdomain 更改为您需要的。显然也要更新用户凭据。

<?
$whmusername = "root";
$whmpassword = "12345luggage";
$subdomain = "mysubdomain";
$rootdomain = "example.com";

$query = "https://127.0.0.1:2087/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=".$subdomain."&rootdomain=".$rootdomain;

$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($whmusername.":".$whmpassword) . "\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;

?>

关于php - 如何使用 cURL 创建子域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31534861/

相关文章:

javascript - 在 PHP 生成的特定行 ID 之前追加

在 MySQL 中插入日期时间时的 PHP date() 格式

seo - 多语言索引和 SEO juice

apache - htaccess 使用子域和 https 重定向

php - 在 Sphinx 中启用部分单词匹配?

php - Laravel - 通过检查数据库中数组中是否存在值来过滤集合

PHP CURL SSL 连接到 XML 网关

PHP 在单独的线程中发送 POST 请求并忘记

c - 使用 libcurl 使 https get

dns - 我们如何在 Domain Dns 中使用 host as @ 添加多个 TXT 记录?