使用 PHP 创建 Confluence Wiki 页面

Using PHP to create Confluence Wiki pages

我已经能够使用 PHP 来生成 JIRA 问题,但想知道是否也可以创建 link 回到 JIRA 问题的 Confluence wiki 页面。我还没能完成这项工作。有没有人有任何例子说明这是如何实现的?

您可以使用 REST API 轻松创建新的 Confluence 页面。下面是一个使用 curl 的例子:

curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/

接下来要做的就是 PHP 调用它。检查以下示例:

完成后,您还可以在 Confluence 页面中嵌入一个 JIRA 宏以进行额外的调整。这意味着原始的 curl 将添加 JIRA 宏作为存储格式(在 Confluence 页面中,单击工具(或现在“...”)和 select 查看存储格式 举个例子。

这是 JIRA 宏的示例:

<ac:structured-macro ac:name="jira">
  <ac:parameter ac:name="server">Example JIRA</ac:parameter>
  <ac:parameter ac:name="serverId">fdsafds-68es-3615-a6f7-71427b983092</ac:parameter>
  <ac:parameter ac:name="key">XYZ057-172</ac:parameter>
</ac:structured-macro>

您需要弄清楚您的 server 名称、serverId 和 JIRA 问题 key 你自己。

这意味着原始卷曲看起来像这样:

curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page with a JIRA macro added:</p><ac:structured-macro ac:name="jira"><ac:parameter ac:name="server">Example JIRA</ac:parameter><ac:parameter ac:name="serverId">fdsafds-68es-3615-a6f7-71427b983092</ac:parameter><ac:parameter ac:name="key">XYZ057-172</ac:parameter></ac:structured-macro>","representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/

使用 php 中的代码,您可以创建 Confluence 页面:

<?php

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"page\",\"title\":\"inserttitle\",\"space\":{\"key\":\"insertspace\"},\"ancestors\":[{\"type\":\"page\",\"id\":insertancestor}],\"body\":{\"storage\":{\"value\":\"<p>This is a new page</p>\",\"representation\":\"storage\"}}}");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "insertusername" . ":" . "insertpassword");

    $headers = array();
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);

    ?>