PHP Curl发布没有标题的文件 [英] PHP Curl post a file without header

查看:71
本文介绍了PHP Curl发布没有标题的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个PHP的人,但是尝试将cli CURL调用移植到PHP……最初我有:

I'm not much of a PHP person, but trying to port a cli CURL call to PHP ... originally I had:

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

我试图将其转换为:

<?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);
?>

问题在于,这导致额外的标头被添加到目的地,其中包含------------------------------6614b0cf8aae Content-Disposition: form-data; name=Filedata; filename=$filepath Content-Type: application/octet-stream.

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.

我认为这是因为使用$data['Filedata']的方式,所以我尝试用下面的内容替换CURLOPT_POSTFIELDS行,但这只是发布路径字符串而不是文件本身.

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.

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

有人介意指出我所缺少的吗?就像我说的,不是PHP的人!

Anyone mind pointing out what I'm missing? Like I said, not a PHP person!

推荐答案

只需直接加载文件内容即可.当前,您正在执行键/值解析后的POST数据,该数据与原始的curl命令行不同.

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.

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

这篇关于PHP Curl发布没有标题的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆