Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Redmine:使用API发布附件(或不使用)_Php_Api_Redmine_Attachment - Fatal编程技术网

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

Php Redmine:使用API发布附件(或不使用),php,api,redmine,attachment,Php,Api,Redmine,Attachment,有没有办法从外部PHP脚本在Redmine中发布问题的附件?如果API不支持这一点(我在wiki上没有找到任何东西),那么还有其他方法吗 到目前为止,我只尝试过这个,但它不起作用: $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 Firef

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

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

$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];
}
   }
$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”);
卷曲关闭($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=数组(
“密码”=>“1234”,
'返回url'=>'http%3A%2F%2F192.168.1.115%2Fredmine%2F',
“用户名”=>“管理员”,
“真实性令牌”=>$token,
'登录'=>'登录»'
);
打印(数据2美元);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data2);
$out=curl\u exec($ch);
回音$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=数组(
“键”=>“104a2e2b72d4f5d184775d8324c2e0cb6386815e”,
'is_private'=>'0',
“跟踪器id”=>“1”,
'subject'=>'此错误是从我的API发送的',
'description'=>'这是一个说明',
'status_id'=>'0',
“优先级id”=>“4”,
'分配给\u id'=>'',
“家长问题id”=>“
);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$out=curl\u exec($ch);
回音$out;
卷曲关闭($ch);
函数getToken(){
$url=”http://192.168.1.115/redmine/login"; 
$input=@file\u get\u contents($url)或die(“无法访问文件:$url”);
$regexp=“”;
if(preg_match_all($regexp,$input,$matches))
{ 
返回$matches[1][0];
}
}

您可以链接方法附加\u文件。插件中有一个将屏幕截图粘贴到wiki页面/问题的示例

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
模块RedmineScreenshotPaste
def自带(基本)
base.send(:extend,ClassMethods)
基本类\u评估do
下面是使用redmine对php的简单curl调用。
**redmine/redmine_curl.php中的flie类文件**
**示例文件Example.php**

很抱歉,我错过了您想从PHP scipt发布附件的消息:(
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);
?>