0

I am doing Jira REST API calls and I am wondering how I can dynamically add more than one component to the components field using REST API in PHP. I have the following code, works when I set it static, but not sure how to do it dynamically.

Example of static component set:

$data = array(

'fields' => array(

    'project' => array(

        'key' => $rowAnswers["Key"]

    ),
    'summary' =>  $rowAnswers["Summary"],

    'description' => $rowAnswers["Description"],

    'issuetype' => array(
        'name' => $rowAnswers["IssueType"]
    ),

    'components' => array(
        array(
            "name" => "component1"
        ),
        array(
            "name" => "component2"
        )
    )
),
);

My array that I want to replace the static content with:

$components = explode(",", $rowAnswers["Components"]);
        $arr = array();
        foreach($components as $value){
            $array = array("name"=>$value);
            array_push($arr,$array);
        }

Replacing

'components' => array(
    array(
        "name" => "component1"
    ),
    array(
        "name" => "component2"
    )
)

with

'components' => [
                    $arr
                ]

doesn't work, I get:

"{"error":false,"error_msg":"","data":"{\"errorMessages\":[],\"errors\":{\"components\":\"expected Object\"}}"}"

I see on an api call to get a request it looks like this:

[components] => Array
            (
                [0] => stdClass Object
                    (
                        [name] => component1
                    )

                [1] => stdClass Object
                    (
                        [name] => component2
                    )

            )

But I am unsure how to transform an array into this type of object or request in PHP. Calling with PHP-cURL and json_encoding the data it sends.

Thanks in advance!

Curtis
  • 1
  • 4
  • how do you view your api response ? `json_decode()` ?? – hassan Feb 05 '17 at 18:26
  • Yes, I am using PHP cURL to both get the data as well as create. the viewing is done by: $result = curl_exec($ch); json_decode($result); @HassanAhmed – Curtis Feb 05 '17 at 18:29

2 Answers2

0

you need to decode your json as associative array by setting the second parameter to true

check out the json_decode

assoc

When TRUE, returned objects will be converted into associative arrays.
hassan
  • 7,812
  • 2
  • 25
  • 36
  • I am not having an issue when reading the data, It is when I am sending the data via php-curl in a rest api call to create a request. I need to have the currently static component array use the dynamically set array from a DB. – Curtis Feb 05 '17 at 18:35
  • This is an example: https://answers.atlassian.com/questions/9379363/how-to-add-component-while-creating-an-issue-via-jira-rest-api – Curtis Feb 05 '17 at 18:38
  • as i get from the link you have provided that you have to send the data as php an associative array not objects array , your response seems to be an object array not an associative array , decoding the json as associative array may help! , if that doesn't make a sense please provide more details to your question – hassan Feb 05 '17 at 18:56
  • I need to replace this section in my request: 'components' => array( array( "name" => "component1" ), array( "name" => "component2" ) ) with the data from my dynamic array from the DB. 'components' => [$arr] doesn't work, not sure what I need. – Curtis Feb 05 '17 at 19:03
  • in your code , try to replace the static part by : `'components' => $arr` , and for debugging purpose , print your array to check if it follow the jira request standards or not – hassan Feb 05 '17 at 19:12
  • I did that before and got an error, so I also added $array = json_decode(json_encode(array("name"=>$value)), FALSE); when creating the array of arrays, now it works. thanks. – Curtis Feb 05 '17 at 19:15
0

To fix this I had to do the following:

When creating the array from the DB:

$components = explode(",", $rowAnswers["Components"]);
        $arr = array();
        foreach($components as $value){
            $array = json_decode(json_encode(array("name"=>$value)), FALSE);
            array_push($arr,$array);
        }

Then to set the component in the request:

'components' => $arr

Thanks

Curtis
  • 1
  • 4