Confluence : send API rest GET/POST/PUT requests in Json format – PHP

1. Finding a Page By Title and Space Key*

curl -u admin:admin -X GET "http://localhost:8080/confluence/rest/api/content?title=myPage%20Title&spaceKey=TST&expand=history" | python -mjson.tool

PHP :

// Création d'une ressource cURL
$curl = curl_init();

// Définition de l'URL et autres options appropriées
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_USERPWD, "login:password");

//désactive les tests du SSL
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

// url de destination
curl_setopt($curl, CURLOPT_URL, "https://my_server/rest/api/content?title=my_title&spaceKey=my_space&expand=space,body.view,version,container");

// Récupération de l'URL et passage au navigateur
$json = curl_exec($curl);
$resultStatus = curl_getinfo($curl);

//si requete OK
if($resultStatus['http_code'] == 200)
{
//decodage du résultat json
$obj = json_decode($json);

//si la page existe
if($obj->{'size'} == 1)
{ //actions }
}
curl_close($curl);

2. Creating a new page

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/ | python -mjson.tool

PHP : 

// Création d'une ressource cURL
$curl = curl_init();

// Définition de l'URL et autres options appropriées
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($curl, CURLOPT_USERPWD, "login:password");

//désactive les tests du SSL
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

//données
$data = '{"type":"page","title":"my title","space":{"key":"my space"},"ancestors":[{"type":"page","id":"123456"}],"body":{"storage":{"value":"this is my content","representation":"storage"}}}';

//données de type json et envoi en POST
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POST, true);

//envoi des données
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//url de l'API
curl_setopt($curl, CURLOPT_URL, "https://my_server/rest/api/content");

//exécution
$json = curl_exec($curl);
$resultStatus = curl_getinfo($curl);

if($resultStatus['http_code'] == 200) echo "Page créée <br />";
else echo "Création de la page : ECHEC (code " . $resultStatus['http_code'] . ")<br />";
curl_close($curl);

3. Updating a page

curl -u admin:admin -X PUT -H 'Content-Type: application/json' -d'{"id":"3604482","type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is the updated text for the new page</p>","representation":"storage"}},"version":{"number":2}}' http://localhost:8080/confluence/rest/api/content/3604482 | python -mjson.tool

PHP : 

… [code « get page from title » 1.*] …

//get page id and version
$res = $obj->{'results'};
$idPage = $obj->{'results'}[0]->{'id'};
$versionPage = $obj->{'results'}[0]->{'version'}->{'number'} + 1;

//données
$data = '{"id":"'. $idPage .'","type":"page","title":"my title","space":{"key":"my space"},"ancestors":[{"type":"page","id":"123456"}],"body":{"storage":{"value":"This is my new content","representation":"storage"}},"version":{"number":'. $versionPage .'}}';

//données de type json et envoi en POST
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');

//envoi des données
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//url de l'API
curl_setopt($curl, CURLOPT_URL, "https://my_server/rest/api/content/$idPage");

//exécution
$json = curl_exec($curl);
$resultStatus = curl_getinfo($curl);

if($resultStatus['http_code'] == 200) echo "Page modifiée <br />";
else echo "Modification de la page : ECHEC (code " . $resultStatus['http_code'] . ")<br />";
curl_close($curl);