how to call confluence rest api using php?

Vidya Shevale October 15, 2018

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
Solutions Partner
Solution Partners provide consulting, sales, and technical services on Atlassian products.
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
AUG Leaders

Atlassian Community Events