2017-04-19 112 views
0

我有一個cURL命令,它在CLI(通過git Bash)中工作得非常好。見下:將cURL的語法轉換爲php cURL

curl -D- -k -o tvdata.xls -u adminid:adminpw -X GET -H "Content-Type: application/vnd.ms-excel" https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident 

這個命令從我的JIRA導出excel文件(xls)中的所有問題。 現在我想將這個命令轉換爲php curl。我曾嘗試下面這段代碼:

$url = 'https://build.bnum.laposte.fr/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident'; 

    $username ='adminid'; 
    $password ='adminpw'; 

    $file = fopen('tvdata.xls', 'w'); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/vnd.ms-excel")); 
    curl_setopt($ch, CURLOPT_NOPROGRESS, FALSE); 
    curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 15000); 
    curl_setopt($ch, CURLOPT_FILE, $file); 

    curl_exec($ch); 
    curl_close($ch); 

    fclose($file); 

但是當我運行此PHP代碼,他創建一個只excel文件,但沒有得到一個錯誤。如果有人可以找出這個問題,請?

在此先感謝

Achillix

+0

設置CURLOPT_RETURNTRANSFER爲1 – diavolic

+0

@diavolic THX爲您的答案,但沒有變化 – achillix

回答

1

您是否嘗試過使用在線轉換器像一個this

從...

curl -D- -k -o tvdata.xls -u adminid:adminpw -X GET -H "Content-Type: application/vnd.ms-excel" https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident 

...你:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/ 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, "https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 

curl_setopt($ch, CURLOPT_USERPWD, "adminid" . ":" . "adminpw"); 

$headers = array(); 
$headers[] = "Content-Type: application/vnd.ms-excel"; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

$result = curl_exec($ch); 
if (curl_errno($ch)) { 
    echo 'Error:' . curl_error($ch); 
} 
curl_close ($ch); 

...您的示例代碼,CURLOPT_URL和CURLOPT_HTTPHEADER相比是不同的,和其他一些選項未設置。

+0

thx爲您的迴應和你的幫助。轉換器非常棒!我試過你的代碼,現在我收到一條錯誤消息: 錯誤SSL證書問題:證書鏈中的自簽名證書 cURL它是選項-k來證明SSL問題。任何想法? THX – achillix

+0

我已經添加了這2個選項,它的工作原理如下:curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);但你以99%解決了我的問題。非常感謝。乾杯 – achillix