【问题标题】:Using Confluence REST API to post generated html table (PHP) without getting http 400 status code?使用 Confluence REST API 发布生成的 html 表(PHP)而不获取 http 400 状态码?
【发布时间】:2015-09-03 14:50:27
【问题描述】:

我的目标是使用提供的 REST API 在 confluence wiki 中创建一个包含表格的页面,该 wiki 用户可以在页面创建后使用 WYSIWYG 编辑器轻松编辑该表格。

我把文本拆开,分门别类放入数组数组中,然后从中生成一个html表格(一个字符串),效果很好。

但是将这个原始 html 表(在 $htmlTable 中)作为内容发布到 REST API

$data = array("type" => "page", "title" => someTitle, 
       "space" => array("key" => "$uploadSpace"), 
       "body" => array("storage" => array("value" => "$htmlTable", "representation" => "storage")));

返回 400 statusCode 错误。显然是因为使用 htmlspecialchars 将字符串编码为 html 没有正确转义输入,但是我将如何创建一个结构化的输入来转换为 html 表?

我尝试使用 confluence 宏传递我的 html 表,该宏将 html 输入呈现到表中以供查看:

$data = array("type" => "page", "title" => someTitle,
       "space" => array("key" => "$uploadSpace"), 
       "body" => array("storage" => array("value" => "<ac:structured-macro ac:name=\"html\"><ac:plain-text-body><![CDATA[$htmlTable]]></ac:plain-text-body></ac:structured-macro>", "representation" => "storage")));

这会在页面上呈现我的 html 表格,但是这无法满足 WYSIWYG 要求中轻松编辑的要求,因为 wiki 的用户之后会看到包含在宏中的 html 代码。

非常感谢您。

【问题讨论】:

    标签: php html rest confluence confluence-rest-api


    【解决方案1】:

    您需要修改您的表格以使用Confluence Wiki Markup 来标记表格。以html表为例:

    $data = "<table>
             <tr><th>First Name</th><th>Last Name</th><th>GPA</th></tr>
             <tr><td>Bob</td><td>Jones</td><td>3.52</td></tr>
             <tr><td>Fred</td><td>Smith</td><td>2.89</td></tr>
             </table>";
    

    应该写成:

    $data = "||First Name||Last Name||GPA||
             |Bob|Jones|3.52|
             |Fred|Smith|2.89|";
    

    然后,当您通过 REST 进行更新/插入时,将“表示”从“存储”更改为“维基”。 (并删除 html 宏包装器)

    $data = array("type" => "page", "title" => someTitle,
                 "space" => array("key" => "$uploadSpace"), 
                 "body" => array("**wiki**" => array("value" => "$htmlTable", "representation" => "**wiki**")));
    

    这将为您提供一个原生的、所见即所得的 Confluence 表。

    'storage' 表示表示 body 测试已经转换为 Confluence 内部用于存储内容的格式。您想使用“wiki”插入,并让 Confluence 为您处理任何转换。

    【讨论】:

      【解决方案2】:

      经过长时间的努力,找到了解决办法。

      在您的 HTML 内容上添加宏代码。

      $bodyFormatted = '<ac:structured-macro ac:name="html"><ac:plain-text-body><![CDATA['.$bodyFormatted.']]></ac:plain-text-body></ac:structured-macro>';
      

      $bodyFormatted 变量保存 HTML 内容。

      $data['type'] = 'page';
      $data['title'] = $articleData["title"].time();
      $data['space']['key'] = $articleData["container"];
      $data['body']['storage']['value'] = $bodyFormatted;
      $data['body']['storage']['representation'] = 'storage';
      

      编码为 json 并通过 REST 传递。在那个重要的过程之前,我失败了。

      “启用 HTML 宏” 参考:https://confluence.atlassian.com/conf55/confluence-user-s-guide/creating-content/using-the-editor/working-with-macros/html-macro

      尽管有警告,请启用上述模块 (html (html-xhtml))

      处理您的 API 调用,然后检查内容。希望你完成了。工作完成后,禁用模块,以防止跨脚本攻击。

      【讨论】:

        猜你喜欢
        • 2020-02-25
        • 1970-01-01
        • 1970-01-01
        • 2021-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-08
        相关资源
        最近更新 更多