Creating an A-Record with Plesk API

自作多情 提交于 2020-01-16 18:46:35

问题


I modified the example PHP script for the Plesk API enough that I think it SHOULD be working to create an A-Record DNS, however whenever I run the script it simply times out after about 2-3 minutes. The Parallels Development forums seem to be a ghost town, so I am hoping maybe someone here might be able to spot the issue.

<?php

/** Reports error during API RPC request */
class ApiRequestException extends Exception {}

function DNSCreateRequest()
{
    $xmldoc = new DomDocument('1.0', 'UTF-8');
    $xmldoc->formatOutput = true;

    // <packet>
    $packet = $xmldoc->createElement('packet');
    $packet->setAttribute('version', '1.6.3.1');
    $xmldoc->appendChild($packet);

    // <packet/dns>
    $dns = $xmldoc->createElement('dns');
    $packet->appendChild($dns);

    // <packet/dns/add_rec>
    $addrec = $xmldoc->createElement('add_rec');
    $dns->appendChild($addrec);

    // add_rec elements
        $addrec->appendChild($xmldoc->createElement('domain_name','fragnet.net'));
    $addrec->appendChild($xmldoc->createElement('type','A'));
    $addrec->appendChild($xmldoc->createElement('host','testdns'));
    $addrec->appendChild($xmldoc->createElement('value','127.0.0.1'));

    //$xmldoc->saveXML($addrec);

    return $xmldoc;
}

/** Prepares CURL to perform the Panel API request */
function curlInit($host, $login, $password)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://{$host}:8443/enterprise/control/agent.php");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST,           true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER,

    array("HTTP_AUTH_LOGIN: {$login}",
        "HTTP_AUTH_PASSWD: {$password}",
        "HTTP_PRETTY_PRINT: TRUE",
        "Content-Type: text/xml")
    );

    return $curl;
}

/** Performs a Panel API request, returns raw API response text */
function sendRequest($curl, $packet)
{
    curl_setopt($curl, CURLOPT_POSTFIELDS, $packet);
    $result = curl_exec($curl);

    if (curl_errno($curl))
    {
        $errmsg  = curl_error($curl);
        $errcode = curl_errno($curl);
        curl_close($curl);
        throw new ApiRequestException($errmsg, $errcode);
    }

      curl_close($curl);
      return $result;
}

/** Looks if API responded with correct data */
function parseResponse($response_string)
{
    $xml = new SimpleXMLElement($response_string);

    if (!is_a($xml, 'SimpleXMLElement'))
        throw new ApiRequestException("Cannot parse server response: {$response_string}");

    return $xml;
}

/** Check data in API response */
function checkResponse(SimpleXMLElement $response)
{
    $resultNode = $response->dns->add_rec->result;

    // check if request was successful
    if ('error' == (string)$resultNode->status)
        throw new ApiRequestException("The Panel API returned an error: " . (string)$resultNode->result->errtext);
}

// int main()

$host = 'www.mydomainishere.net';
$login = 'myusername';
$password = 'mypassword';

$curl = curlInit($host, $login, $password);

try
{
    $response = sendRequest($curl, DNSCreateRequest()->saveXML());
    $responseXml = parseResponse($response);
    checkResponse($responseXml);
}
catch (ApiRequestException $e)
{
    echo $e;
    die();
}

// Explore the result

foreach ($responseXml->xpath('/packet/dns/add_rec/result') as $resultNode)
{
    echo "DNS id: " . (string)$resultNode->id . " ";
    //echo (string)$resultNode->data->gen_info->name . " (" . (string)$resultNode->data->gen_info->dns_ip_address . ")\n";
}

?>

回答1:


I was able to determine that it would not accept "domain_name" and instead wanted a "site-id" instead. Once this was used, my script worked flawlessly.



来源:https://stackoverflow.com/questions/11175816/creating-an-a-record-with-plesk-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!