Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

how to call confluence rest api using php?

Vidya Shevale October 15, 2018 edited

require_once 'vendor/autoload.php';

Unirest\Request::auth('xxx', 'xxx');
$headers = array( 'Accept' => 'application/json', 'Content-Type' => 'application/json' );
$body = '
{
  "id": "1234",
  "title": "new apge",
  "type": "page",
  "space": {
    "key": "page"
  },
  "status": "current",
  "ancestors": [
    {
      "id": "page"
    }
  ],
  "body": {
    "view": {
      "value": "22",
      "representation": "view"
    },
    "export_view": {
      "value": "23",
      "representation": "view"
    },
    "styled_view": {
      "value": "24",
      "representation": "view"
    },
    "storage": {
      "value": "25",
      "representation": "view"
    },
    "editor2": {
      "value": "26",
      "representation": "view"
    },
    "anonymous_export_view": {
      "value": "27",
      "representation": "view"
    }
  }
}
';

$response = Unirest\Request::post( 'http://localhost:8090/confluence/rest/api/content', $headers, $body );
var_dump($response);


above is my code and i am getting header of the confluence in return in broken html format

I want actual response which mentioned in confluence documentation so that I can implement further operations.

reply me as soon as possible
TIA

1 answer

0 votes
david
Atlassian Partner
October 15, 2018

Hi @Vidya Shevale this is the code you need to use in PHP (simple HTTPRequest, no Unirest) to call Confluence with the POST Content operation as you specified:

<?php

$request = new HttpRequest();
$request->setUrl('http://confluenceURL/rest/api/content');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
'cache-control' => 'no-cache',
'Content-Type' => 'application/json'
));

$request->setBody('{
"id": "1234",
"title": "new page",
"type": "page",
"space": {
"key": "page"
},
"status": "current",
"ancestors": [
{
"id": "page"
}
],
"body": {
"view": {
"value": "22",
"representation": "view"
},
"export_view": {
"value": "23",
"representation": "view"
},
"styled_view": {
"value": "24",
"representation": "view"
},
"storage": {
"value": "25",
"representation": "view"
},
"editor2": {
"value": "26",
"representation": "view"
},
"anonymous_export_view": {
"value": "27",
"representation": "view"
}
}
}');

try {
$response = $request->send();

echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}

Be aware that you need to specify the right body format to create content, everything is detailed here: https://docs.atlassian.com/ConfluenceServer/rest/6.12.0/#content-createContent

Kind regards,

David

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, likes for trees, atlassian community, social impact, tree planting campaign, community kudos, atlassian giving, environmental impact, sustainability, likes for good, atlassian social responsibility, community challenge

Make every click count—help us plant 50,000 trees! 🌳

Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!

Help us plant more trees
AUG Leaders

Atlassian Community Events