我正在尝试在Jira中创建一个问题。
我可以使用正确的响应发出GET请求,但是当我发出POST请求时会出现问题。这是代码。
<?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;
&GT;
我收到以下错误:
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的替代品,因为谷歌应用引擎不支持卷曲。
答案 0 :(得分:3)
http_build_query()
生成一个url编码的字符串。但是,API需要JSON。您应该使用json_encode()
代替。
变化:
$data = http_build_query($data);
要:
$data = json_encode($data);
虽然可能不是您唯一的问题,但这肯定是导致 400 Bad Request 的一个问题。
答案 1 :(得分:0)
你的代码中可能有拼写错误吗?
$data=array('fields' => array ('project' => array ('key' => 'WOIS',),'summary' => 'ABC','description' => 'ABC','issuetype' => array ('name' => 'Task',),),);
VS
$data = ['fields' => ['projects'=>['key'=>'WOIS'],'summary'=>'adfsdf','description'=>'adfefsa','issuetype'=>['name'=>'Task']]];
第一行显示project
,而第二行显示projects