Отправка JSON через API cURL в PHP: искаженный JSON - PullRequest
0 голосов
/ 24 июня 2019

Я пытаюсь отправить данные через REST API в Confluence, чтобы создать там вопрос.Поэтому мне нужно отправить данные в формате JSON на сервер.До сих пор я всегда получал HTTP 400 в качестве ответа.С помощью json_last_error () я понял, что у меня неправильно сформирован JSON в моих $ jsonData.Единственное исправление, которое я нашел для этого, состоит в том, чтобы заменить «на». Я пытался, но он все еще не работает. У вас есть какие-либо другие советы для меня? Заранее большое спасибо !!

<?php

//API Url
$url = 'https://***.net/confluence/rest/questions/1.0/question';

//Initiate cURL
$ch = curl_init($url);

//The JSON data
$jsonData = array(
"title" => "abc",
"body" => "def",
"dateAsked" => "2019-06-06",
"spaceKey" => "LMLMLM"
);

//The JSON header
$jsonHeader = array (
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Basic ***'
);

//Encode the array into JSON
$jsonDataEncoded = json_encode ($jsonData);

//Tell cURL that we want to send a POST request
curl_setopt ($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields
curl_setopt ($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

//Attach Header
curl_setopt ($ch, CURLOPT_HTTPHEADER, $jsonHeader);

//Execute the request
$json_response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
    die("Error: call to URL $ch failed with status $status, response 
$json_response, curl_error " . curl_error($ch) . ", curl_errno " . 
curl_errno($ch));
}

curl_close ($ch);

$response = json_decode($json_response, true);
?>
...