curl — обновить страницу с помощью PHP переменных / REST API Confluence

Я хочу обновить страницу в месте слияния с некоторыми php-переменными. Итак, вот мой PHP-код для обновления страницы:

$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"),
));

Довожу до вашего сведения:

  • Если я напечатаю $ post в другом скрипте, появится значение переменной

  • без переменной сеанс curl работает и я могу обновить страницу

Вот ошибка:

{"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"}

0

Решение

Неправильно сформированы значения json для создаваемого вами сообщения. Почему вы не используете json_encode (); объявив все значения внутри массива, вы можете изменить свой код следующим образом:

$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'
),
));
1

Другие решения

Других решений пока нет …