最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How do i post data to url with fields?

programmeradmin1浏览0评论

I am trying to post some data to a website called Jira and create a new issue there with the data I send. I did try it with postman and it works! But now I just need to implement this in code. So far, this is what I managed to do and I know it is wrong but would like to know where and how to resolve it.

$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 ('',$pload);

In the variable $bdy it should be like this in json format:

{
"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"
   }
 }
}

For instance the path from 'key' => 'LD' should really be something like this project=>key=>'LD', but I don't know how to path it in WP/PHP.

I am trying to post some data to a website called Jira and create a new issue there with the data I send. I did try it with postman and it works! But now I just need to implement this in code. So far, this is what I managed to do and I know it is wrong but would like to know where and how to resolve it.

$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/rest/api/2/issue',$pload);

In the variable $bdy it should be like this in json format:

{
"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"
   }
 }
}

For instance the path from 'key' => 'LD' should really be something like this project=>key=>'LD', but I don't know how to path it in WP/PHP.

Share Improve this question asked Sep 23, 2020 at 11:44 ThinsantaThinsanta 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You have to build the array as the json array needs to be

$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/rest/api/2/issue',$pload);
发布评论

评论列表(0)

  1. 暂无评论