With a simple call by "get" using CURL php library, we can connect to the Confluence documentation manager and link this documentation to your website. Let's see how we can do it.

The first thing to do is read the Confluence REST API Documentation. In it you will be able to see the structure that we need in the URLs, the parameters and the data that we can obtain from confluence such as the HTML content of a page, the tree menu, etc ...

The Confluence REST API is protected by the same restrictions that are provided through the standard Confluence web interface. This means that if we don't log in, you are accessing Confluence anonymously. Also, if we are logged in and you don't have permission to see something in Confluence, you won't be able to see it using the Confluence REST API either.

The confluence authentication mode is based is basic authentication . This method sends a username and password in the headers of the request to connect.

Let's see an example. Imagine that your username is user_confluence and your password !con_pass_957:

1- Create a string with the username and password: user_confluence: !con_pass_957, Now encode the string using base64.

2- To encode the string in base64 we use the function base64_encode:

<?php
echo base64_encode("user_confluence:!con_pass_957");
?>

The result of this string will be this: dXNlcl9jb25mbHVlbmNlOiFjb25fcGFzc185NTc=

3- Now we add this string to the request headers:

curl -D- \
   -X POST \
   -H "Authorization: Basic dXNlcl9jb25mbHVlbmNlOiFjb25fcGFzc185NTc" \
   -H "Content-Type: application/json" \
   "https://confluenc.domain/rest/api/content/123?expand=body.view"

This request will return, in JSON format, the HTML content of the page with ID 123.

We can do a connection test using the free program Advanced REST client:

Advanced REST client

Once we know how to connect to confluence, we are going to pass this curl command to PHP code. The following example shows how to get the HTML content of a page:

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEFILE, '');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, '');

$header = array();
$header[] = "Authorization: Basic " . base64_encode("user_confluence:!con_pass_957");
$header[] = "Content-Type: application/json";

$page_id = 123;

$url = "https://confluence.domain.com/rest/api/content/" . $page_id . "?expand=body.view";

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);

$result = curl_exec( $ch );

print_r(json_decode($result, true));

As you can see, in the example, we make a call to confluence using curl and the url https://confluence.domain.com/rest/api/content/123?expand=body.view

The confluence REST API offers different URL structures and parameters to get the different content. For example:

  • Get the HTML content of a page: https://confluence.domain.com/rest/api/content/123?expand=body.view
  • Get the child pages of a parent page: https://confluence.domain.com/rest/api/content/123/child?expand=page
  • Get attachments: https://confluence.domain.com/rest/api/content/123/child/attachment . The attachments of a page are usually the images that are included within it.
  • Get the tags of a page: https://confluence.domain.com/rest/api/content/123/label

In this way, we can obtain the content of all the pages, images and labels of a complete confluence space.

In the HTML that is obtained from the confluence page, the url of the images points to the confluence url. Ex: https://confluence.domain.com/download/attachments/123456789/image2020-2-18_12-54-33.png?version=1&modificationDate=1584111935000&api=v2

Note: These images will not be accessible since you will not be connected to confluence directly. To solve this we can download all the page images and modify the url using DOM. Ex:

/**
 * GET CONTENT
 * Function to get the content of page in confluence
 * @param int $page_id
 * @return array
 */
public function getContent($page_id = null)
{
    $response = array();
    if($page_id) {

        $url = $confluence_url . "/rest/api/content/" . $page_id . "?expand=body.view";

        $page = $this->get_result($url); //Connect to confluence to get the content

        if($page) {

            $this->getAttachements($page_id); //Here we donwload the images of the page

            //Use DOM to modify the url of the images
            $doc = new DOMDocument();
            $doc->loadHTML(mb_convert_encoding($page['body']['view']['value'], 'HTML-ENTITIES', 'UTF-8'), LIBXML_NOWARNING);

            $tags = $doc->getElementsByTagName('img');//Get all img tags
            foreach ($tags as $tag) {
                $filename = $tag->getAttribute('data-linked-resource-default-alias'); //Get file name
                $new_src_url = $this->confluence_images_dir . $page_id . '/' . $filename ; //We add the new url
                $tag->setAttribute('src', $new_src_url); //Apply the new url
            }
            $response['content'] = $doc->saveHTML();
            $response['title'] = $page['title'];
        }
    }
    return $response;
}

Here's a complete class used as a library in the CodeIgniter php framework.

https://github.com/fjfran/php_connect_confluence