简体   繁体   English

curl CLI与php curl

[英]curl CLI versus php curl

I have worked with php Curl for a while now, I recently stumbled upon a piece of curl CLI which I can't get to work in PHP curl. 我已经使用php Curl了一段时间,最近我偶然发现了一个curl CLI,但我无法在PHP curl中使用它。

The CLI curl looks as followed: CLI卷曲如下所示:

curl -qgsSkH "Content-Type: multipart/form-data"
--no-progress-bar
--header "X-Api-Token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
-F "filename=@c/tmp/test.txt"
-F "options={\"application\":\"2\",\"timeout\":\"500\",\"priority\":\"0\"}"
https://xxxaddrxxx:443/API/vers1.0/sub

I use the following PHP curl code to mimic the above CLI curl: 我使用以下PHP curl代码来模仿上述CLI curl:

$file = new CURLFile($fileEntry->getStoragePath());
$file->setMimeType($fileEntry->mime);
$file->setPostFilename($fileEntry->original_filename);

$data = array();
$data['options'] = json_encode($postData);
$data['filename'] = $file;

$headers = array();
$headers[] = "Content-Type: multipart/form-data";
$headers[] = 'X-FeApi-Token: '.$this->authToken;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

$curl_response = curl_exec($curl);

When I output the $data array it will give the following: 当我输出$ data数组时,它将给出以下内容:

array:2 [
  "options" => "{"application":"2","priority":"0","timeout":"5000"}"
  "filename" => CURLFile {
    +name: "/etc/tmp/5645db226dbddINVOICE-722.doc"
    +mime: "application/msword"
    +postname: "INVOICE-722.doc"
  }
]

Which is all the correct data corresponding to what I provide in the code. 这是我在代码中提供的所有正确数据。

Never the less the server where I try to push the file and meta data too respond with an error that the options array or filename is not given in or uploaded. 无论如何,在我尝试推送文件和元数据的服务器上,也总是响应未提供或上传选项数组或文件名的错误。 This error is only given when I try to push it through PHP when I run the raw curl command on a Linux server it works just fine. 仅当我在Linux服务器上运行raw curl命令时,尝试通过PHP推送该错误时,才会出现此错误。

Do I make a mistake regarding the double -F option? 关于double -F选项,我是否出错? Or do I missed something in the new CURLFile method of posting, because in all previous post on Stack Overlow the deprecated method using '@' is used? 还是我在新的CURLFile发布方法中错过了某些东西,因为在Stack Overlow上的所有先前发布中,都使用了不推荐使用的'@'方法?

The header part of the curl cli command and php curl seems to be identical when debugging. curl cli命令和php curl的标头部分在调试时似乎是相同的。 The payload itself can't easily be verified due to the ssl encryption (https, which can't be turned off on the receiving server). 由于ssl加密(https,无法在接收服务器上将其关闭),因此无法轻松地验证有效负载本身。

Thank you in advance. 先感谢您。

G. G。

This issue is fixed. 此问题已解决。 The CURL works perfectly it was the Vendors API integration which was not acting as expected. CURL完美运行,这是因为Vendors API集成没有按预期进行。

It asks for a JSON options variable but what was not documented is that the ORDER is of importance which is just totally unacceptable if you request a JSON. 它要求一个JSON选项变量,但没有记录的是ORDER的重要性,如果您请求JSON,这是完全不可接受的。

The vendor is informed and will patch this issue because when doing detailed investigation it turned out there way of parsing the JSON can have some security consequences. 供应商将被告知并将解决此问题,因为在进行详细调查时,发现解析JSON的方法可能会带来一些安全后果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM