Search code examples
phpwikiconfluence

Using PHP to create Confluence Wiki pages


I've been able to use PHP in order to generate JIRA issues, but was wondering if it is possible to also create Confluence wiki pages that link back to the JIRA issue. I haven't been able to make this work yet. Does anyone have any examples on how this is accomplished?


Solution

  • You can easily create a new Confluence page using the REST API. Here's an example using 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/
    

    The next thing to do would be for PHP to call that. Check the following for examples:

    Having done that, you could also embed a JIRA macro in the Confluence page for extra zing. That would mean that the original curl would add the JIRA macro as the storage format (in a Confluence page, click on Tools (or "..." now) and select View Storage Format for an example.

    Here's an example of the JIRA macro:

    <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>
    

    You'll need to figure out your server name, serverId & JIRA issue key yourself.

    This would mean the original curl looks like this:

    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/