将语法从cURL转换为php cURL

时间:2017-04-19 12:54:31

标签: php excel curl jira

我有一个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 个答案:

答案 0 :(得分: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不同,并且未设置其他一些选项。