Redmine: Post attachments using API (or not)
Is there any way to post attachments to issues in Redmine from an outside PHP script? If API doesnt support this (i didnt find anything on the wiki) then is there another way?
So far i have tried only this, but it doesnt work:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://192.168.1.115/redmine/login");
$useragent="Mozilla/5.0 (Windows NT 5.1; rv:8.0a2) Gecko/20110927 Firefox/8.0a2";
curl_setopt($ch, CURLOPT_COOKIEJAR, "C:\\xampp\\htdocs\\redmine\\cookie.txt");
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, "http://192.168.1.115/redmine/login");
curl_setopt($ch, curlopt_post, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "C:\\xampp\\htdocs\\redmine\\cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$token = getToken();
$data2 = array(
'password' => '1234',
'back_url' => 'http%3A%2F%2F192.168.1.115%2Fredmine%2F',
'username' => 'admin',
'authenticity_token' => $token,
'login' => 'Login Β»'
);
print_r($data2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data2);
$out = curl_exec($ch);
echo $out;
curl_exec($ch);
curl_setopt($ch, CURLOPT_COOKIEJAR, "C:\\xampp\\htdocs\\redmine\\cookie2.txt");
$useragent="Mozilla/5.0 (Windows NT 5.1开发者_如何学Python; rv:8.0a2) Gecko/20110927 Firefox/8.0a2";
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Redmine-API-Key: 104a2e2b72d4f5d184775d8324c2e0cb6386815e'));
curl_setopt($ch, CURLOPT_URL, "http://192.168.1.115/redmine/projects/lvx/issues/new");
curl_setopt($ch, CURLOPT_COOKIEFILE, "C:\\xampp\\htdocs\\redmine\\cookie2.txt");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = array(
'key' => '104a2e2b72d4f5d184775d8324c2e0cb6386815e',
'is_private' => '0',
'tracker_id' => '1',
'subject' => 'This bug was sent from my API',
'description' => 'this is a description',
'status_id' => '0',
'priority_id' => '4',
'assigned_to_id' => '',
'parent_issue_id' => ''
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$out = curl_exec($ch);
echo $out;
curl_close($ch);
function getToken(){
$url = "http://192.168.1.115/redmine/login";
$input = @file_get_contents($url) or die("Could not access file: $url");
$regexp = "<input name=\"authenticity_token\" type=\"hidden\" value=\"(.+?)\" />";
if(preg_match_all("$regexp", $input, $matches))
{
return $matches[1][0];
}
}
You can chain method attach_files. There is example from plugin which pastes screenshot to wiki-page/issue.
module RedmineScreenshotPaste
def self.included(base)
base.send(:extend, ClassMethods)
base.class_eval do
class << self
alias_method_chain :attach_files, :screenshot
end
end
end
module ClassMethods
def attach_files_with_screenshot(obj, attachments)
if attachments.is_a?(Hash)
screenshot = attachments['screenshot']
if screenshot.is_a?(Hash)
file = UploadedScreenshot.new(screenshot.delete('content'),
screenshot.delete('name'))
screenshot['file'] = file
end
end
attach_files_without_screenshot(obj, attachments)
end
end
end
Following is simple curl call for php with redmine.
**flie class file in redmine/redmine_curl.php**
<?php # Redmine Api
class class_redmine{
function get_upload_token($filecontent){
global $redmine_url , $redmine_key;
$upload_url = $redmine_url.'uploads.json?key='.$redmine_key;
$request['type'] = 'post';
$request['content_type'] = 'application/octet-stream';
//$filecontent = file_get_contents('test.php');
return $token = $this->curl_redmine($upload_url,$request,$filecontent);
//$token->upload->token;
}
#Issue
function create_issue($post_data){
global $redmine_url , $redmine_key;
$issue_url = $redmine_url.'issues.json?key='.$redmine_key;
$request['type'] = 'post';
$request['content_type'] = 'application/json';
return $this->curl_redmine($issue_url,$request,$post_data);
}
function get_issue($issue_id='',$project_id=''){
global $redmine_url , $redmine_key;
if($project_id!=''){
$issue_url = $redmine_url.'issues.json?key='.$redmine_key.'&project_id='.$project_id;
}else{ $issue_url = ($issue_id=='')?$redmine_url.'issues.json?key='.$redmine_key : $redmine_url.'issues/'.$issue_id.'.json?key='.$redmine_key;
}
return $this->curl_redmine($issue_url,'','');
}
#Projects
function get_projects($project_id=''){
global $redmine_url , $redmine_key;
$proj_url = ($project_id=='')?$redmine_url.'projects.json?key='.$redmine_key : $redmine_url.'projects/'.$project_id.'.json?key='.$redmine_key;
return $this->curl_redmine($proj_url,'','');
}
#Curl
function curl_redmine($redmine_url,$request='',$post_data=''){
if(!isset($request['type'])){ $request['type']=null; }
if(!isset($request['content_type'])){ $request['content_type']=null; }
//Create a curl object
$ch = curl_init();
//Set the useragent
$agent = $_SERVER["HTTP_USER_AGENT"];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
//Set the URL
curl_setopt($ch, CURLOPT_URL, $redmine_url );
if($request['type'] == 'post'){
//This is a POST query
curl_setopt($ch, CURLOPT_POST,1);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//Set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: '.$request['content_type'],
'Content-Length: ' . strlen($post_data))
);
}
//We want the content after the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Follow Location redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/
//curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
//curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
//Execute the action to login
$postResult = curl_exec($ch);
//if($postResult == false){ return $info = curl_getinfo($ch);}
$response = json_decode($postResult);
//echo '<pre>'; print_r($response); echo '</pre>';
return $response;
}
}//class_redmine
?>
**Example file example.php**
<?php
//code for class_settting.php
function get_redmine($methodName='',$data=''){
global $redmine_url , $redmine_key;
//$query='select * from '.VIS_TABLE_PREFIX.'integration where integration_type=37 and is_enabled=1 and domain_id='.VIS_DOMAIN;
//$res = $this->database->query_exec($query);
//$login_integrate=$this->database->fetch_result_array($res);
/*if($login_integrate==-1){ return $login_integrate; }
if(count($login_integrate)>0 && $login_integrate!=-1){
$redmine_url = $login_integrate[0]['billing_url'];
$redmine_username = $login_integrate[0]['admin_user'];
$redmine_password = $login_integrate[0]['admin_password'];
$redmine_key = $login_integrate[0]['api_key'];
}*/
$redmine_url = 'http://localhost/redmine/';
$redmine_key = '41f132773cc29887bc2e4566863aedc01cde6e2b';
include_once('redmine/redmine_curl.php');
$obj_redmine = new class_redmine();
#check Auth
$res = $obj_redmine->get_projects();
if(!isset($res->projects) || (isset($res->total_count) && ($res->total_count)==0)){ return -1; }
switch($methodName){
case 'check_status' : return $login_integrate; ##check redmine integration in vision
break;
##Trackers
//
##Issue statuses
//
##Project
case 'projectAll' : return $obj_redmine->get_projects(); #used
break;
case 'projectById' : return $obj_redmine->get_projects($data['project_id']);
break;
##Users
//
##Issues
case 'showIssue' : return $obj_redmine->get_issue($data['issue_id']);
break;
case 'issueAll' : return $obj_redmine->get_issue();
break;
case 'issueByProjectId' : return $obj_redmine->get_issue('',$data['project_id']);
break;
case 'createIssue' : return $obj_redmine->create_issue($data);
break;
case 'uploadFileToIssue' : return $obj_redmine->get_upload_token($data);
break;
default: return 0;
}
}
$filecontent = file_get_contents('test.php');
$token = get_redmine('uploadFileToIssue',$filecontent);
$filecontent = file_get_contents('Picture.jpg');
$token2 = get_redmine('uploadFileToIssue',$filecontent);
$uploads = array(
array(
'token' => $token->upload->token,
'filename' => 'MyFile.php',
'description' => 'MyFile is better then YourFile...',
'content_type' => 'application/txt',
),
array(
'token' => $token2->upload->token,
'filename' => 'Picture.jpg',
'description' => 'MyFile is better then YourFile...',
'content_type' => 'application/image',
),
);
$custom_fields = array(
array(
'id' => 1,
'name' => 'Phone',
'value' => '1234265689'
),
array(
'id' => 2,
'name' => 'Proj sub name',
'value' => 'Test'
),
);
$post_data = array('issue'=>array(
'project_id' => 4,
'subject' => 'ABCDEFG',
'description' => 'Test',
'uploads' => $uploads,
'custom_fields' => $custom_fields,
),);
$post_data = json_encode($post_data);
#all proj
//$res = get_redmine('projectAll');
#proj by id
//$res = get_redmine('projectById',array('project_id'=>'4'));
#get all issue
//$res = get_redmine('issueAll');
#get issue by id
//$res = get_redmine('showIssue',array('issue_id'=>'85'));
#get issue by project id
//$res = get_redmine('issueByProjectId',array('project_id'=>'5'));
#create issue
$res = get_redmine('createIssue',$post_data);
echo '<pre>';print_r($res);
?>
精彩评论