HTTP 请求失败! HTTP/1.1 400 错误请求

HTTP request failed! HTTP/1.1 400 Bad Request

我正在尝试在 Jira 中创建问题。

我可以发出 GET 请求并得到正确的响应,但是当我发出 POST 时出现问题 request.Here 是代码。

<?php

$userName ='xxxxxxxxxxxxxxxx';
$password ='xxxxxxxxxxxx';

$data = ['fields' => ['projects'=>['key'=>'ABC'],'summary'=>'abc','description'=>'abc','issuetype'=>['name'=>'Task']]];

$data = http_build_query($data);
$context = 
     array("http"=>
        array(
            "method" => "POST",
                "header" => "Authorization: Basic " . base64_encode($userName.':'.$password) . "\r\n".
                'Accept: application/json'."\r\n".
                "Content-Length: ".strlen($data)."\r\n".
                    'Content-Type: application/json'."\r\n",
            'content' => $data,
            'verify_peer'=>false,
            'verify_peer_name'=>false,
            )
            );
$context = stream_context_create($context);
$result = file_get_contents("https://xxxxx.atlassian.net/rest/api/2/issue/", false, $context);
echo "The result is ", $result;

?>

我收到以下错误:

Warning:file_get_contents(https://xxxxx.atlassian.net/rest/api/2/issue/):
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in
/var/www/html/test/new.php on line 27

谁能帮帮我?谢谢

P.S

我不想使用 curl 作为 http-streams 的替代品,因为 google 应用引擎不支持 curl。

http_build_query() generates a url-encoded string. However, the API requires JSON. You should be using json_encode() 代替。

变化:

$data = http_build_query($data);

收件人:

$data = json_encode($data);

虽然这可能不是您唯一的问题,但这绝对是一个会导致 400 Bad Request.

的问题

请问您的代码有错字吗?

$data=array('fields' => array ('project' => array ('key' => 'WOIS',),'summary' => 'ABC','description' => 'ABC','issuetype' => array ('name' => 'Task',),),); 

$data = ['fields' => ['projects'=>['key'=>'WOIS'],'summary'=>'adfsdf','descriptio‌​n'=>'adfefsa','issue‌​type'=>['name'=>'Tas‌​k']]];

第一行说 project 而第二行说 projects