Неправильный заголовок cURL в PHP с REST API Redmine

В настоящее время я пытаюсь создать проблему в Redmine через REST API. Я экспортирую проблемы из Redmine в файл CSV. Затем я создаю XML-файл из CSV, а затем отправляю его в API с помощью cURL. Я устанавливаю некоторые параметры:

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => URL_REDMINE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/xml; charset=utf-8',
'X-Redmine-API-Key: '.KEY_API),
CURLOPT_BINARYTRANSFER => TRUE,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $dataXML,
CURLOPT_SAFE_UPLOAD => TRUE,
CURLOPT_ENCODING => "utf-8"));

curl_exec($curl);
echo json_encode(curl_getinfo($curl));

Итак, я четко говорю, что кодировка должна быть UTF-8. Но когда я печатаю информацию (с curl_getinfo) вот что я получаю:

{"url":"[URL I was targeting]",
"content_type":"text\/html; charset=iso-8859-1", [...]}

(Я удалил некоторые не относящиеся к делу сведения, но если кто-то хочет их полностью, я могу вставить их.)

Итак, это говорит content_type является text\/html; charset=iso-8859-1 когда я установил это как application/xml; charset=utf-8, Есть ли что-то, чего я не понял с REST API?

1

Решение

Ниже приведен простой вызов curl для php с 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.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);
?>
-2

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]