2017-12-29 17 views
0

hier holen i unten unter Verwendung von curl php alle Fragen von Redmine zu holen versuchen ist der Code, den ich verwenden.Wie alle Probleme aus redmine Anwendung mit curl PHP

mein CURL.php: flie Klassendatei 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 

?> 

hier ist mein example.php

<?php 
function get_redmine($methodName='',$data=''){ 
global $redmine_url , $redmine_key; 

$redmine_url = 'http://192.168.12.231:80/'; 
$redmine_key = 'API KEY'; 
include_once('curlcall.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; 

##Project 
case 'projectAll' : return $obj_redmine->get_projects(); #used 
break; 
case 'projectById' : return $obj_redmine->get_projects($data['project_id']); 
break; 

##Issues 
case 'issueAll' : return $obj_redmine->get_issue(); 
break; 
case 'issueByProjectId' : return $obj_redmine->get_issue('',$data['project_id']); 
break; 
case 'uploadFileToIssue' : return $obj_redmine->get_upload_token($data); 
break; 
default: return 0; 
} 
} 

#get all issue 
$res = get_redmine('issueAll'); 

echo '<pre>';print_r($res); 
?> 

Dieser Code gibt nur 25 records.at das Ende meiner Ausgabe, die es sagt, wie gesamt: 201 Offset: 0 Grenze: 25 ich kann nicht verstehen, wie alle Fragen ple erhalten ase Hilfe irgendjemand, ich bin neu für PHP.

Antwort

0

die Antwort ist alles in the API docs, wenn Sie nur, es zu lesen würden stören. Verwenden Sie die Grenze Parameter um mehr Ergebnisse zu erhalten, und die Offset-Parameter mehr

/issues.json?offset=0&limit=100 würde holen die ersten 100 Ausgaben

/issues.json?offset=100&limit=100 würde holt die nächsten 100 Ausgaben zu laden, und so weiter.

... aber das erste, was Sie tun sollten, ist Ihr Code formatiert werden, weil die unformatierte, was zu lesen ist sehr ärgerlich. dann loswerden diese Linie 'Content-Length: ' . strlen ($post_data), weil curl diesen Header automatisch hinzufügen, wenn Sie nicht, und curl wird keine Tippfehler machen, noch wird es die Länge falsch berechnen, im Gegensatz zu uns die Progammer (Curl hat automatisierte Tests auf jedem commit ausgeführt Teil Ihrer Testsuite, um sicherzustellen, Content-Length korrekt ist, ich wette, das ist nicht), dann diesen

if (! isset ($request ['content_type'])) { 
    $request ['content_type'] = null; 
} 

loszuwerden, weil es nie null sein sollte. für die Zukunft, wenn Sie buchstäblich keine Ahnung, was es sein soll ist, setzen Sie ihn auf application/octet-stream, aber auch das ist falsch in diesem Fall, da die API-Dokumentation der einzigen unterstützten POST-Codierungen sind application/json und application/xml sagt, so dass die richtige Sache zu hier tun wäre:

if (isset ($request ['content_type']) && $request ['content_type'] !== 'application/json' && $request ['content_type'] !== 'application/xml') { 
    throw new \InvalidArgumentException ('invalid content-type specified. supported types are "application/json" and "application/xml"'); 
} 
if($request ['type'] === 'post' && !isset($request ['content_type'])){ 
    throw new \LogicException('no content-type was specified for this POST request, which is required. supported types are "application/json" and "application/xml"'); 
} 

wie für alle Fragen zu holen, eine Anfrage zunächst zu sehen, wie viele Probleme es gibt, dann eine zweite Anfrage macht mit limit = number_of_issues, wie diese

if (! $issue_id) { 
    // first we check how many issues there are (OPTIMIZEME: there is probably a more efficient way to do this, for example limit=0 ?) 
    $issues_url .= '&limit=1'; 
    $number_of_issues = $this->curl_redmine ($issue_url, '', '') ['total_count']; 
    $issue_url = substr ($issues_url, 0, - 1) . $number_of_issues; 
} 

Alles zusammen setzen, und Sie erhalten diese

<?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; 
     } 
     if (! $issue_id) { 
      // first we check how many issues there are (OPTIMIZEME: there is probably a more efficient way to do this, for example limit=0 ?) 
      $issues_url .= '&limit=1'; 
      $number_of_issues = $this->curl_redmine ($issue_url, '', '') ['total_count']; 
      $issue_url = substr ($issues_url, 0, - 1) . $number_of_issues; 
     } 
     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'] !== 'application/json' && $request ['content_type'] !== 'application/xml') { 
      throw new \InvalidArgumentException ('invalid content-type specified. supported types are "application/json" and "application/xml"'); 
     } 
     if ($request ['type'] === 'post' && ! isset ($request ['content_type'])) { 
      throw new \LogicException ('no content-type was specified for this POST request, which is required. supported types are "application/json" and "application/xml"'); 
     } 
     // 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'] 
      )); 
     } 
     // 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 

(die auf meinem Teil völlig ungetestet ist, übrigens.)

ein anderes Problem, das Sie beheben sollen, ist, dass Ihr curl Code vollständig ohne Fehlererkennung ausgeführt wird. Beispielsweise gibt curl_setopt bool (false) zurück, wenn beim Festlegen der Option ein Problem aufgetreten ist, und curl_exec() gibt bool (false) zurück, wenn bei der Übertragung ein Problem aufgetreten ist, fügen Sie eine Fehlererkennung hinzu. Diese Funktionen sollten, dass ziemlich einfach machen:

function ecurl_setopt (/*resource*/$ch , int $option , /*mixed*/ $value):bool{ 
    $ret=curl_setopt($ch,$option,$value); 
    if($ret!==true){ 
     //option should be obvious by stack trace 
     throw new RuntimeException ('curl_setopt() failed. curl_errno: ' . return_var_dump (curl_errno ($ch)).'. curl_error: '.curl_error($ch)); 
    } 
    return true; 
} 
function ecurl_exec (/*resource*/$ch):bool{ 
    $ret=curl_exec($ch); 
    if($ret===false){ 
     throw new RuntimeException ('curl_exec() failed. curl_errno: ' . return_var_dump (curl_errno ($ch)).'. curl_error: '.curl_error($ch)); 
    } 
    return $ret; 
} 


function return_var_dump(/*...*/){ 
    $args = func_get_args(); 
    ob_start(); 
    call_user_func_array ('var_dump', $args); 
    return ob_get_clean(); 
} 

und sicher sein, Sie sind kein Speicher undicht ist, können Sie alles rund um einen try{...curl...}finally{curl_close($ch);}, um sicherzustellen, curl_close wickeln immer aufgerufen werden würde, unabhängig davon, welche curl Funktion löst eine Ausnahme.(dies verhindert Speicherlecks)

+0

Ja, es ist da in der API, aber ich habe keine Klarheit, wo soll ich das in meinem Code verwenden. bitte geben Sie mir einen Vorschlag. – mani

+0

@mani ok dann habe ich meine antwort aktualisiert. – hanshenrik