1

I'm trying to post data to a JIRA REST API but I get a blank response back. The code sits in two files. Here's the code located on the jira.json file:

{
"fields": {
            "project":
                        {
                            "id": "19600"
                        },
            "summary": "REST ye merry gentlemen.",
                            "description": "Creating of an issue using project keys and issue type names using the REST API",
                            "issuetype": {
                                                "name": "Task"
                                         }
                }
}

and the php code:

<?php

$jsondata = file_get_contents("jira.json");
$json = json_decode($jsondata,true);

//print_r($json);




$url_send ="http://jira.greentea.co.za:8091/rest/api/2/issue/";
$str_data = json_encode($json);

//print_r($str_data);

function sendPostData($url, $post){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  $result = curl_exec($ch);
  curl_close($ch);  

  if($result)
    {
        return 1;
    }
  else{
        return 0;
       }
  }



$time =  sendPostData($url_send, $str_data);
var_dump($time);
?>

I get a zero int(0) meaning that the result variable returned a blank result. This is the the cURL Command that I converted to php. data will be replaced by the json array:

curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/
1
  • 1
    Have you noticed you've not included the Content-type header in your PHP code? You can also use curl_getinfo() to get the HTTP code, which might give you an idea as to what's wrong. Commented Mar 10, 2016 at 9:55

1 Answer 1

1

If you are sure that you have CURL enabled with your PHP, try to use this snippet from my code:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json','Content-Length: ' . strlen($json_data)));

Let me know if this works for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.