Php HTTP请求失败!HTTP/1.1400错误请求
我试图在吉拉制造一个问题 我能够发出带有正确响应的GET请求,但是当我发出POST请求时,问题就出现了Php HTTP请求失败!HTTP/1.1400错误请求,php,google-app-engine,jira,jira-rest-api,http-streaming,Php,Google App Engine,Jira,Jira Rest Api,Http Streaming,我试图在吉拉制造一个问题 我能够发出带有正确响应的GET请求,但是当我发出POST请求时,问题就出现了 <?php $userName ='xxxxxxxxxxxxxxxx'; $password ='xxxxxxxxxxxx'; $data = ['fields' => ['projects'=>['key'=>'ABC'],'summary'=>'abc','description'=>'abc','issuetype'=>['name'=>
<?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;
有人能帮我吗?谢谢
附言
我不想使用curl代替http流,因为googleappengine不支持curl。生成一个url编码的字符串。但是,API需要JSON。你应该改用
更改:
$data = http_build_query($data);
致:
虽然这可能不是您唯一的问题,但这肯定是一个会导致400个错误请求的问题。您的代码中是否有输入错误
$data=array('fields' => array ('project' => array ('key' => 'WOIS',),'summary' => 'ABC','description' => 'ABC','issuetype' => array ('name' => 'Task',),),);
vs
第一行显示
project
,第二行显示projects
您正在发送一个查询字符串,但声称发送JSON。这可能是你的第一个问题。@JasonMcCreary这里是上面使用curl的例子,你能建议应该是什么帖子url吗?谢谢Jason,你是对的,使用json_encode()解决了这个问题。我还有一个小问题,与$data=array('fields'=>array('project'=>array('key'=>'WOIS',),'summary'=>'ABC','description'=>'ABC','issuetype'=>array('name'=>'Task',),)有什么区别;和$data=['fields'=>['projects'=>['key'=>'WOIS']、'summary'=>'adfsdf'、'description'=>'adfefsa'、'issuetype'=>['name'=>'Task']];因为如果我使用第二个$data,它就无法识别project字段。在我看来,这两种表示都是正确的。谢谢,很好。我会在另一个问题中问这个问题,这样就更清楚了。欢迎来到StackOverflow。这个问题已经有了一个公认的答案,问题不是打字错误。。。
$data = json_encode($data);
$data=array('fields' => array ('project' => array ('key' => 'WOIS',),'summary' => 'ABC','description' => 'ABC','issuetype' => array ('name' => 'Task',),),);
$data = ['fields' => ['projects'=>['key'=>'WOIS'],'summary'=>'adfsdf','description'=>'adfefsa','issuetype'=>['name'=>'Task']]];