如何将数据与字段一起发布到url?

时间:2020-09-23 作者:Thinsanta

我正在尝试将一些数据发布到一个名为Jira的网站,并用我发送的数据在那里创建一个新的问题。我和邮递员试过了,效果很好!但现在我只需要在代码中实现它。到目前为止,这是我设法做到的,我知道这是错误的,但我想知道在哪里以及如何解决它。

$args = array(

\'headers\' => array(
  \'Content-Type\' => \'application/json\',
  \'Authorization\' => \'Basic \'.$apiKey
)
);
$bdy = array(
\'key\' =>\'LD\' ,
\'summary\'=> \'CODING WORKS\',
"description" => "Creating of an issue using project keys and issue type names using the REST API",
"name"=> "Candidate"
);

$pload = array(
\'method\' => \'POST\',
\'timeout\' => 30,
\'redirection\' => 5,
\'httpversion\' => \'1.0\',
\'blocking\' => true,
\'headers\' => $args,
\'body\' => json_encode($bdy)
);
$response = wp_remote_post (\'https://mysite.atlassian.net/rest/api/2/issue\',$pload);
在变量中$bdy json格式应该是这样的:

{
"fields": {
   "project":
   {
      "key": "LD"
   },
   "summary": "CODING WORKS",
   "description": "Creating of an issue using project keys and issue type names using the REST API",
   "issuetype": {
      "name": "Candidate"
   }
 }
}
例如,路径\'key\' => \'LD\' 应该是这样的project=>key=>\'LD\', 但我不知道如何在WP/PHP中对其进行路径设置。

1 个回复
SO网友:Jasper B

您必须按照json数组的需要构建数组

$args = array(
\'headers\' => array(
  \'Content-Type\' => \'application/json\',
  \'Authorization\' => \'Basic \'.$apiKey
)
);
$bdy = array(
    \'fields\'=> array(
      \'project\' => array(
        \'key\' =>\'LD\'
      ),
      \'summary\'=> \'CODING WORKS\',
      \'description\' => \'Creating of an issue using project keys and issue type names using the REST API\',
      \'issuetype\' => array(
        \'name\'=> \'Candidate\'
      )
    )
);


$pload = array(
\'method\' => \'POST\',
\'timeout\' => 30,
\'redirection\' => 5,
\'httpversion\' => \'1.0\',
\'blocking\' => true,
\'headers\' => $args,
\'body\' => json_encode($bdy)
);
$response = wp_remote_post (\'https://mysite.atlassian.net/rest/api/2/issue\',$pload);