Redmine:使用API​​发布附件(或不发布)

时间:2011-09-30 10:50:16

标签: php api redmine attachment

有没有办法从外部PHP脚本发布Redmine中的问题附件?如果API不支持这个(我没有在维基上找到任何东西)那么还有另一种方法吗?

到目前为止,我只尝试过这个,但它不起作用:

$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; 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];
}
   }

2 个答案:

答案 0 :(得分:2)

您可以链接方法attach_files。有插件的例子,它将屏幕截图粘贴到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

答案 1 :(得分:-1)

for