PHP curl响应错误-“无法识别的令牌”

时间:2019-03-06 22:33:07

标签: php curl

我正在尝试使用Atlassian的REST api向Jira中的项目添加版本。如果我从git bash运行它,则效果很好:

curl --request POST  --url 'https://mysite.atlassian.net/rest/api/3/version'  --header 'Authorization: Basic my_access_token' --header 'Accept: application/json'  --header 'Content-Type: application/json'  --data "{\"name\": \"19.16.0\",\"projectId\": 10011}"

但是当我尝试通过PHP执行相同的操作时,出现错误。我不太熟悉在PHP中使用curl,所以我希望这个问题真的很简单。

这是我的代码:

$versionStr = $_POST['version'];
$project = $_POST['project'];
$url = "https://mysite.atlassian.net/rest/api/3/version";
$headers = array(
    "Content-Type:application/json",
    "Accept: application/json",
    "Authorization: Basic my_access_token"
);


//The data you want to send via POST
$fields = array(
    "name"      => $versionStr,
    "projectId" => $project
);

//url-ify the data for the POST
$fields_string = http_build_query($fields);

//open connection
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
//set the url, number of POST vars, POST data

curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

// these error logs display the expected data
error_log("+++headers: " . print_r($headers, 1));
error_log("+++fields count: " . count($fields));;
error_log("+++fieldString: " . $fields_string);


//execute post
$result = curl_exec($ch);
error_log("+++Curl Result: " . print_r($result, 1));
curl_close($ch);

这是错误,如$result中返回:

{"errorMessages":["Unrecognized token 'name': was expecting 'null', 'true', 'false' or NaN\n at [Source: org.apache.catalina.connector.CoyoteInputStream@4dcb2cb6; line: 1, column: 6]"]}

我要去哪里错了?我假设这是我的格式问题,而不是端点问题,因为如前所述,通过原始curl调用发送相同的数据是可行的。

0 个答案:

没有答案