Update a Page with PHP Variables / REST API

Chris Nowaczyk August 14, 2018

Hello everybody, I want to update a page in confluence with some php-variables. So here's my PHP Code to update the page:

 

$curl = curl_init();

$post = "{\"id\":\"65604\",\"type\":\"page\",\"title\":\"page\",\"space\":{\"key\":\"***\"},\"body\":{\"storage\":{\"value\":\"<p>Here comes the other variable: $product_response </p>\",\"representation\":\"storage\"}},\"version\":{\"number\":11}}";

curl_setopt_array($curl, array(
  CURLOPT_PORT => "6003",
  CURLOPT_URL => "http://localhost:6003/rest/api/content/65604",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => $post,
  CURLOPT_COOKIE => "JSESSIONID=3A16CBFE8B99E619D62BD4CD6573F184",
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic xyYS123_test_test-45Sdasds==",
    "content-type: application/json"
  ),
));

 

For your information:

  • If i print the $post in another script, the variable value show up
  • without the variable, the curl session works and I can update the page

Thats the error:

{"statusCode":500,"message":"org.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value\n at [Source: com.atlassian.confluence.plugins.restapi.filters.LimitingRequestFilter$1@5dff62ce; line: 1, column: 119]","reason":"Internal Server Error"}

 

I tried many of formats in the $post, but nothing seems to work.

1 answer

1 accepted

0 votes
Answer accepted
Chris Nowaczyk August 15, 2018

For your information:

I asked the same question at stackoverflow. (https://stackoverflow.com/questions/51856253/update-a-page-with-php-variables-rest-api-confluence)

They helped me in a few minutes, you have to use json_encode.

Heres the answer:

The json values for post that you are forming is incorrectly formed, Why dont you use json_encode(); by declaring all the values inside an array, you can change your code like this:

$curl = curl_init();$post = array(
    "id"=>"65604",
    "type"=>"page",
    "title"=>"page",
    "space"=>["key"=>"***"],
    "body" =>["storage"=>["value"=>"<p>Here comes the other variable: ".$product_response." </p>", "representation"=>"storage"]],
    "version"=>["number"=>11]
);
curl_setopt_array($curl, array(CURLOPT_PORT => "6003",CURLOPT_URL => "http://localhost:6003/rest/api/content/65604",CURLOPT_RETURNTRANSFER => true,CURLOPT_ENCODING => "",CURLOPT_MAXREDIRS => 10,CURLOPT_TIMEOUT => 30,CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,CURLOPT_CUSTOMREQUEST => "PUT",CURLOPT_POSTFIELDS => json_encode($post),CURLOPT_COOKIE => "JSESSIONID=3A16CBFE8B99E619D62BD4CD6573F184",CURLOPT_HTTPHEADER => array(
"authorization: Basic xyYS123_test_test-45Sdasds==",
"content-type: application/json",
    'Accept: application/json'
),
));

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events