dongtidai6519 2015-11-30 12:49
浏览 61
已采纳

curl CLI与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.

The CLI curl looks as followed:

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:

$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:

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.

Do I make a mistake regarding the double -F option? 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?

The header part of the curl cli command and php curl seems to be identical when debugging. The payload itself can't easily be verified due to the ssl encryption (https, which can't be turned off on the receiving server).

Thank you in advance.

G.

  • 写回答

1条回答 默认 最新

  • dp152153 2015-12-10 11:20
    关注

    This issue is fixed. The CURL works perfectly it was the Vendors API integration which was not acting as expected.

    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.

    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.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵