2017-06-06 83 views
0

我發現了幾個關於如何將附件上載到jira中的問題的例子,但是我還沒有能夠使它們中的任何一個工作。我在Jira社區幫助論壇上發佈了這個問題,但已經過了一個多星期的0回覆,希望這裏的社區能夠提供幫助。Jira附加文件使用PHP和CURL發佈

這裏是我當前的嘗試:

$Jirausername = 'myUsername'; 
    $Jirapassword = 'myPassword'; 

    $ch=curl_init(); 
$headers = array(
    'X-Atlassian-Token: nocheck', 
    'Content-Type: multipart/form-data' 
); 
$data = array('file' => "testing.txt"); 


curl_setopt_array(
    $ch, 
    array(
     CURLOPT_URL=>'https://myCompany.net/rest/api/latest/issue/TAG-78/attachments', 
     CURLOPT_POST=>true, 
     CURLOPT_VERBOSE=>1, 
     CURLOPT_POSTFIELDS=>$data, 
     CURLOPT_SSL_VERIFYHOST=> 0, 
     CURLOPT_SSL_VERIFYPEER=> 0, 
     CURLOPT_RETURNTRANSFER=>true, 
     CURLOPT_HEADER=>false, 
     CURLOPT_HTTPHEADER=> $headers, 
     CURLOPT_USERPWD=>"$Jirausername:$Jirapassword" 
    ) 
); 
$result=curl_exec($ch); 
$ch_error = curl_error($ch); 
if ($ch_error) { 
    echo "cURL Error: $ch_error"; 
} else { 
    var_dump($result); 
} 
curl_close($ch); 

testing.txt是在同一個目錄作爲該文件。我已經卷曲安裝在哪裏,這是託管,可以創建JIRA罰款問題的Web服務器,只是不能似乎上傳attahcments ...

當我運行這個頁面會顯示:

string(0) "" 

沒有附件上傳也不用說了。任何想法我做錯了什麼?

編輯:添加賞金,這裏有一些事情我已經嘗試:

  1. 努力都NOCHECK和NOCHECK
  2. 努力都@ testing.txt和testing.txt
  3. 刪除「內容型:多部分/格式數據」
  4. 完整路徑,例如:$data = array('file'=>"@C:\xampp\htdocs\Website\testing.txt ,'filename'=>'testing.txt');
  5. 試過這樣太因爲已知捲曲差錯的:$data = array('file'=>"@C:\xampp\htdocs\Website\testing.txt" ';filename=testing.txt');

以及上述各項的組合。無論我嘗試什麼,它都不起作用。也確保我是Jira的管理員級用戶。我覺得我的代碼應該工作......但顯然不是。

+0

你可以嘗試將'file'重命名爲'file_contents': '$ data = array('file_contents'=>「testing.txt」);'? – grundic

+0

@grundic完成。輸出改爲:string(2)「[]」仍然沒有文件上傳。 – Lain

+0

[這裏是](https://confluence.atlassian.com/jirakb/how-to-attach-an-attachment-in-a-jira-issue-using-rest-api--699957734.html)標準的一個例子cUrl,但到目前爲止我還沒有發現任何區別。啊,'multipart/form-data'並不是Atlassian的例子。 – grundic

回答

0

我最初的假設是錯誤的:它的工作原理與兩個NOCHECKNOCHECK - 沒關係。

而不是把一個文件名作爲參數file,你必須首先創建一個捲曲的文件對象是這樣的:

$cfile = curl_file_create('testing.txt'); 

然後放入數組:

$data = array('file' => $cfile); 

這裏完整的解決方案,這對我有效:

<?php 
$Jirausername = '<username>'; 
$Jirapassword = '<password>'; 

$ch=curl_init(); 
$headers = array(
    'X-Atlassian-Token: nocheck', 
    'Content-Type: multipart/form-data' 
); 


$cfile = curl_file_create('testing.txt'); 
$data = array('file' => $cfile); 


curl_setopt_array(
    $ch, 
    array(
     CURLOPT_URL=>'https://<JIRA-SERVER>/rest/api/latest/issue/<ISSUE-KEY>/attachments', 
     CURLOPT_POST=>true, 
     CURLOPT_VERBOSE=>1, 
     CURLOPT_POSTFIELDS=>$data, 
     CURLOPT_INFILESIZE => 5, 
     CURLOPT_SSL_VERIFYHOST=> 0, 
     CURLOPT_SSL_VERIFYPEER=> 0, 
     CURLOPT_RETURNTRANSFER=>true, 
     CURLOPT_HEADER=>true, 
     CURLOPT_HTTPHEADER=> $headers, 
     CURLOPT_USERPWD=>"$Jirausername:$Jirapassword" 
    ) 
); 
$result=curl_exec($ch); 
$ch_error = curl_error($ch); 
if ($ch_error) { 
    echo "cURL Error: $ch_error"; 
} else { 
    var_dump($result); 
} 
curl_close($ch); 
?> 
+0

沒有區別。無核查和nocheck都無所作爲。曾經嘗試過。 – Lain

+0

它絕對必須是'no-check'。那可能是另一回事了。 – grundic

+0

謝謝你試圖幫助我。這是我身邊真正的痛苦。試圖將tests.txt的@ sign infront同時用於no-check和nocheck。就在我沒有在我的帖子中提到這一點。 – Lain