将语法从cURL转换为php cURL

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

  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 
</ code> </ pre>

此命令可以很好地导出来自我的JIRA的excel文件(xls)中的所有问题。
我想将此命令转换为php curl。 我在下面尝试了以下代码:</ p>

  $ 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) ;
</ code> </ pre>

但是当我运行这个PHP代码时,他只创建一个空</ strong> excel文件,但没有得到错误。 如果有人能解决这个问题吗?</ p>

提前致谢</ p>

Achillix </ p>
</ div>

展开原文

原文

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

douzheng0702
douzheng0702 THX为您的答案,但没有变化
3 年多之前 回复
dongpai2468
dongpai2468 将CURLOPT_RETURNTRANSFER设置为1
3 年多之前 回复

1个回答



您是否尝试过使用在线转换器,例如这个?</ p>

From ... </ p>

  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 
</ code> </ pre>

...你得到:</ p>

  //由curl-to-PHP生成:http://incarnate.github.io/curl-to-php/
$ch = curl_init();

ncurl_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” );
; ncurl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,“GET”);

ncurl_setopt($ ch,CURLOPT_USERPWD,“adminid”。“:”。“adminpw”); \ n
$ headers = array();
$ headers [] =“Content-Ty pe:application / vnd.ms-excel“;
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);

$ result = curl_exec($ ch);
if(curl_errno($ ch)){
echo' 错误:'。 curl_error($ ch);
}
curl_close($ ch);
</ code> </ pre>

...与示例代码相比,CURLOPT_URL和CURLOPT_HTTPHEADER不同,有些 其他选项未设置。</ p>
</ div>

展开原文

原文

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.

douwen5690
douwen5690 我添加了这两个选项,它可以工作:curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false); 但是你在99%的时候解决了我的问题。 非常感谢你。 干杯
3 年多之前 回复
doushouxie7064
doushouxie7064 感谢您的回复和帮助。 转换器太棒了! 我尝试了你的代码,现在我收到一条错误消息:错误ssl证书问题:cURL证书链中的自签名证书它是选项-k来解决SSL问题。 任何的想法? THX提前
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问