2015-10-14 216 views
0

我想使用JIRA API進行curl操作登錄。使用curl進行登錄認證

我在捲曲操作中遇到問題。我在JIRA_URL中有主URL。'/ demo/111'; $ username和$ password的值正確地在函數中傳遞,但顯示狀態爲'失敗'。我的捲曲代碼中是否存在任何問題

function JIRA_authenticate($username, $password) { 

    $url = JIRA_URL . '/demo/111'; 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); 
    curl_setopt($curl, CURLOPT_URL, $url); 

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // ssl ensure cert 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); /// ssl ensure cert 

    $issue_list = (curl_exec($curl)); 
    echo $issue_list; 

    return $issue_list; 
} 
+0

你正在運行本地機器嗎? –

+0

是在本地機器上運行 – user3386779

+0

使用[Guzzle](https://github.com/guzzle/guzzle)這種類型的調用庫,它很容易 – Saqueib

回答

1

您沒有提到您試圖用此代碼實現的目標。 您是否試圖獲取機票信息,發佈問題?你只是使用API​​ ...

那麼這裏是一個腳本,與JIRA API一起工作。

<?php 
$username = 'test'; 
$password = 'test'; 
$url = "https://xxxxx.xxxxxxx.net/rest/api/2/project"; 

$ch = curl_init(); 

$headers = array(
    'Accept: application/json', 
    'Content-Type: application/json' 
); 
$test = "This is the content of the custom field."; 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 

//curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_URL, $url); 

curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
$result = curl_exec($ch); 
$ch_error = curl_error($ch); 

if ($ch_error) { 
    echo "cURL Error: $ch_error"; 
} else { 
    echo $result; 
} 

curl_close($ch); 
?> 

此代碼正在從JIRA獲取項目。 如果您想創建問題,您必須將REST URL更改爲/ rest/api/2/issue /,並使用「POST」而不是「GET」方法。