2016-03-02 18 views
6

Jira REST APIを使用したいと思いますが、失敗しました。Jira REST APIを使用する

のJIRAサイトは、次のリクエストURLを与える:

curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/ 

そして、これはcollector.jsonというファイルに配置されてJSON配列、次のとおりです。

"fields": [ 
      { 
       "project": 
         { 
          "key": "ATL" 
         }, 
       "summary": "REST ye merry TEST.", 
       "description": "Creating of an issue using project keys and issue type names using the REST API", 
       "issuetype": { 
           "name": "Task" 
          } 
      } 
     ] 

}

コードは次のとおりです。

<?php 
include_once "collector.php"; 

$jiraValues = jsonArray("collector.json"); 
$jira_url = "http://jira.howareyou.org:8091/rest/api/2/issue/createmeta"; 
$jiraString = json_encode($jiraValues); 

$request = curl_init($jira_url); // initiate curl object 
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response 
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1) 
curl_setopt($request, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 

curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
curl_setopt($request, CURLOPT_ENCODING, ""); 
curl_setopt($request, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($request, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($request, CURLOPT_POSTFIELDS, $jiraString); // use HTTP POST to send form data 
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. 
curl_setopt($request, CURLOPT_URL, $jira_url); 

$json_raw = curl_exec($request); // execute curl post and store results in $json_raw 

curl_close($request); // close curl object 

// This line takes the response and breaks it into an array using the specified delimiting character 
$jira_response = json_decode($json_raw, TRUE); 

print_r($jira_response); 

私はそれを実行すると、何も起こりません。私はフィードバックを得ません。

私はhereを見つけ、有効な情報に置き換えました。

+0

あなたは 'cURL' reqからの応答を受け取りますか? – Pogrindis

+0

'さらに、' print_r'の代わりにJSONを呼び出し元に 'echo'してください。 – Pogrindis

+0

@Pogrindis私はそれをエコーし​​ようとしましたが、まだ結果はありません。私はそのcURL reqで何を置き換える必要があるのか​​分かりません。 – kya

答えて

1

まず助けるためにいくつかの有用なグローバルを定義します。

:我々はそれがとても似ている使用する方法を次に

function post_issue($data) { 
    $jdata = json_encode($data); 
    $ch = curl_init(); 
    curl_setopt_array($ch, array(
     CURLOPT_POST => 1, 
     CURLOPT_URL => JIRA_URL . '/rest/api/2/issue/createmeta' . $resource, 
     CURLOPT_USERPWD => USERNAME . ':' . PASSWORD, 
     CURLOPT_POSTFIELDS => $jdata, 
     CURLOPT_HTTPHEADER => array('Content-type: application/json'), 
     CURLOPT_RETURNTRANSFER => true 
    )); 
    $result = curl_exec($ch); 
    curl_close($ch); 
    return json_decode($result); 
} 

define('JIRA_URL', 'http://jira.howareyou.org:8091'); 
define('USERNAME', ''); 
define('PASSWORD', ''); 

はその後、我々は、POSTメソッドを定義する必要があります新しい問題を作成する

$new_issue = array(
    'fields' => array(
     'project' => array('key' => 'TIS'), 
     'summary' => 'Test via REST', 
     'description' => 'Description of issue goes here.', 
     'priority' => array('name' => 'Blocker'), 
     'issuetype' => array('name' => 'Task'), 
     'labels' => array('a','b') 
    ) 
); 

が私たちの前に作られた機能新しい問題に渡して呼び出します。

$result = post_issue($new_issue); 
if (property_exists($result, 'errors')) { 
    echo "Error(s) creating issue:\n"; 
    var_dump($result); 
} else { 
    echo "New issue created at " . JIRA_URL ."/browse/{$result->key}\n"; 
} 

REST

注意を経て、新たな問題を掲示する基本的な例:のJIRA APIのURLがわずか

を設定する必要があるかもしれませんが
+0

データはjson配列ですか? – kya

+0

@kyaはい、確かに更新されました!直接JSON配列を渡す場合は、 '$ new_issue'を使用しないでください。 – Pogrindis

+0

次のエラーが発生します:この行の配列から文字列への変換CURLOPT_URL => JIRA_URL。 '/ rest/api/2/issue/createmeta'のようになります。$ resourceと次のエラー:警告:最初のパラメータは、オブジェクトまたはこの行の既存のクラスの名前でなければなりません:if(property_exists($ result、 'errors')) – kya

関連する問題