ds11111111111111111
2017-04-19 12:54
浏览 108

将语法从cURL转换为php cURL

I have a cURL command which works very well en CLI (via git Bash). See below:

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

This command export very well all issues in an excel file (xls) from my JIRA. Now i want to transform this command to php curl. I have tried this code below:

    $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);

but when i run this php code, he create an empty excel file only, but without to get an error. If somebody can figure out this problem please?

Thanks in advance

Achillix

图片转代码服务由CSDN问答提供 功能建议

我有一个cURL命令,它可以很好地用于CLI(通过git Bash)。 见下文:

  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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanca3415 2017-04-19 13:00
    已采纳

    Have you tried using on-line converters like this one?

    From...

    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
    

    ...you get:

    // 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);
    

    ...compared with your sample code, CURLOPT_URL and CURLOPT_HTTPHEADER are different, and some other options are not set.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题