Plesk api String无法解析为XML

时间:2012-04-03 12:13:21

标签: php xml api curl plesk

我想使用Plesk Api for PHP。我从Parallels网站下载了一个样本,并尝试将其用于我的网站。当我在我的网站上打开页面时,我收到以下错误:

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML

我使用的代码:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
/**
* Reports error during API RPC request
*/
class ApiRequestException extends Exception {}

/**
* Returns DOM object representing request for information about all available domains
* @return DOMDocument
*/

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

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

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

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

      // <packet/domain/get/filter>
      $filter = $xmldoc->createElement('filter');
      $get->appendChild($filter);

      // <packet/domain/get/dataset>
      $dataset = $xmldoc->createElement('dataset');
      $get->appendChild($dataset);

      // dataset elements
      $dataset->appendChild($xmldoc->createElement('limits'));
      $dataset->appendChild($xmldoc->createElement('prefs'));
      $dataset->appendChild($xmldoc->createElement('user'));
      $dataset->appendChild($xmldoc->createElement('hosting'));
      $dataset->appendChild($xmldoc->createElement('stat'));
      $dataset->appendChild($xmldoc->createElement('gen_info'));

      return $xmldoc;
}
/**
* Prepares CURL to perform Plesk API request
* @return resource
*/
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 Plesk API request, returns raw API response text
*
* @return string
* @throws ApiRequestException
*/
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
*
* @return SimpleXMLElement
* @throws ApiRequestException
*/
function parseResponse($response_string)
{
      $xml = new SimpleXMLElement($response_string);
      if (!is_a($xml, 'SimpleXMLElement'))
             throw new ApiRequestException("Can not parse server response: {$response_string}");
      return $xml;
}
/**
* Check data in API response
* @return void
* @throws ApiRequestException
*/
function checkResponse(SimpleXMLElement $response)
{
      $resultNode = $response->domain->get->result;

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

//
// int main()
//
$host = '************';
$login = '************';
$password = '************';

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

try {

      $response = sendRequest($curl, domainsInfoRequest()->saveXML());
      $responseXml = parseResponse($response);
      checkResponse($responseXml);

} catch (ApiRequestException $e) {
      echo $e;
      die();
}

// Explore the result
foreach ($responseXml->xpath('/packet/domain/get/result') as $resultNode) {
      echo "Domain id: " . (string)$resultNode->id . " ";
      echo (string)$resultNode->data->gen_info->name . " (" . (string)$resultNode->data->gen_info->dns_ip_address . ")\n";
}

?>

我希望有人可以帮我找到解决方案。

1 个答案:

答案 0 :(得分:0)

您的脚本与我的服务器完美配合(尽可能恢复格式化)。 它似乎基于原始Parallels文档中提供的示例。所以我从`Plesk integration guide抓住了一个例子并应用到我的服务器 - 它也可以。

我可以假设您的Plesk服务器有些错误配置。如果您的打印XML请求(domainsInfoRequest()->saveXML())和XML响应($response),也许您可​​以对其进行故障排除。出于某种原因,显然您的$ response包含与有效XML代码不同的内容。如果不确定,您可以将其复制/粘贴到文件中并在其上运行xmllint(XML验证工具)。