ds11111111111111111 2017-04-19 12:54
浏览 109
已采纳

将语法从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

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料