简体   繁体   English

PHP Curl发布没有标题的文件

[英]PHP Curl post a file without header

I'm not much of a PHP person, but trying to port a cli CURL call to PHP ... originally I had: 我不是一个PHP人士,但尝试将cli CURL调用移植到PHP……最初我有:

curl -i -H "Content-Type: text/csv" -X POST --data-binary @$filepath $destination

I've tried to turn this into: 我试图把它变成:

<?php
$data['Filedata'] = "@".$filepath;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $destination.'/_');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/csv"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch); 
curl_close($ch);
?>

The problem is that this results in an extra header being prepended to the destination, containing ------------------------------6614b0cf8aae Content-Disposition: form-data; name=Filedata; filename=$filepath Content-Type: application/octet-stream. 问题在于,这会导致在目标之前添加一个额外的标头,其中包含------------------------------6614b0cf8aae Content-Disposition: form-data; name=Filedata; filename=$filepath Content-Type: application/octet-stream. ------------------------------6614b0cf8aae Content-Disposition: form-data; name=Filedata; filename=$filepath Content-Type: application/octet-stream.

I assumed this was because of the way I used $data['Filedata'] , so I tried replacing the CURLOPT_POSTFIELDS line with the below, but that just winds up posting the path string rather than the file itself. 我以为这是因为我使用$data['Filedata'] ,所以我尝试将CURLOPT_POSTFIELDS行替换为下面的内容,但这只是发布路径字符串而不是文件本身。

curl_setopt($ch, CURLOPT_POSTFIELDS, "@".$filepath);

Anyone mind pointing out what I'm missing? 有人指出我想念什么吗? Like I said, not a PHP person! 就像我说的,不是PHP的人!

Just load the file content directly and it will do. 只需直接加载文件内容即可。 Currently you are doing key/value pared POST data which is different from your original curl commandline. 当前,您正在执行键/值解析的POST数据,该数据与原始的curl命令行不同。

curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filepath));

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

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