Need help creating an issue using REST api
Added by William Blake over 11 years ago
Hello all,
I am a REST newbie and need some help getting started. I have a web form that provides a user three options. Two of the options require an issue to be created in redmine. This web form is not hosted in the same directory as redmine but is on the same domain. When the user clicks one of the options on the page, the form submits the data in the form (basically an id) to a php script that updates a record in our database (separate from redmine) and should post a new issue to redmine under a specific project.
The directions say:
POST /issues.xml
<issue>
<project_id>1</project_id>
<subject>Example</subject>
<priority_id>4</priority_id>
</issue>
This provides very little help to me. From within the php script how do I post data to the Redmine REST api?
Thanks!
Will
Replies (1)
RE: Need help creating an issue using REST api (PHP) - Added by William Blake over 11 years ago
I have figured out a solution that worked for me (hopefully this helps someone else). As a new PHP developer I was just not thinking about things correctly. What I had to do was use curl to post the data to the url using my API key.
So:
$data = array();
// had to create the string this way to make sure it got valid json format
$data['issue'] = array("project_id" => 5, "subject" => "test", "priority_id" => 2);
$data_string = json_encode($data);
$ch = curl_init('https://host/redmine/projects/projectname/issues?key=keyid');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//I am posting to https which has some certificate issues, I needed to set the following to false to ignore the issue
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
// Result seems to return the date time stamp you created the issue.