Confluence Rest API Example Using Laravel

This PHP Lumen tutorial help to consume Confluence API With PHP. The Confluence is an Atlassian product.Its an open and shared workspace for teams.Confluence is provide functionality to create, organize and discuss work with your team.

The confluence is a collaboration tool. It is used to help teams to collaborate and share knowledge efficiently. Using Confluence, users can create blogs and pages which can be commented on and edited by all the members of a team.

What is confluence used for?

This Confluence is a collaboration wiki tool used to help teams to collaborate and share knowledge efficiently. With confluence, we can capture project requirements, assign tasks to specific users, and manage several calendars at once with the help of Team Calendars add-on.

How To Access Confluence Functionality Using Rest API

The Confluence providing rest interface to access resources.You can create page, upload file, share page etc using rest API.

your application will make an HTTP request to Confluence Rest API and parse the JSON(default format) response.You can make GET, PUT, POST and DELETE HTTP Requests to access resources.

How To Create Page Using Confluence Rest API

We will create a new page using confluence rest api. First create a HTTP Post type request to create page to the rest server.

I am Using Lumen micro-framework to consume confluence Rest API.

How To Create Client

We will create a private method, that will use to create connection with Confluence server.


var $confluenceAPI = "http://localhost:8080/confluence/rest/api/";
 
private function _client($endpoint) {
$base64 = base64_encode(username+ ":" + password);
 $client = new Client([
	  'base_uri' => $endpoint,
	  'timeout' => 300,
  'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json", "Basic " + base64_encode(username+ ":" + password)
],
	  'http_errors' => false,
	  'verify' => false
  ]);
  return $client;
}

where is : username and password is you api credentials to access Confluence api.The $confluenceAPI variable is use to hold the confluence API host server name.

Service Method to Create Page

We will create service method to create page,Created a client method for rest api instance and post data to confluence server.

public function createPage() {
$parameters = array("type"=>"page", "title"=>"Test Page", "Space"=>array("key"=>"TST"), "body"=>array("storage"=>array("value"=>"This is a my new test page","representation"=>"storage")));
;
      $client = $this->_client($this->confluenceAPI);
      try {
         $response = $client->post("content/", ['json' => $parameters])->getBody()->getContents();
         return json_decode($response);
      } catch(Exception $ex) {
         Log::info($ex->getMessage());
         return 'Unable to create page';
         
      }
      
      return 'Unable to create page';
   }

We have created a page data variable and converted that data into json format, then we have sent data with request to confluence rest server to create page.The confluence api validated all posted data and create page, they will send confirmation message if the everything is fine otherwise send error message in json format.