本文翻译自:Using curl to upload POST data with files

I would like to use cURL to not only send data parameters in HTTP POST but to also upload files with specific form name. 我想使用cURL不仅在HTTP POST中发送数据参数,而且还上传具有特定表单名称的文件。 How should I go about doing that ? 我应该怎么做呢?

HTTP Post parameters: HTTP Post参数:

userid = 12345 filecomment = This is an image file userid = 12345 filecomment =这是一个图像文件

HTTP File upload: File location = /home/user1/Desktop/test.jpg Form name for file = image (correspond to the $_FILES['image'] at the PHP side) HTTP文件上传:文件位置= /home/user1/Desktop/test.jpg文件的表单名称= image(对应于PHP端的$ _​​FILES ['image'])

I figured part of the cURL command as follows: 我认为cURL命令的一部分如下:

curl -d "userid=1&filecomment=This is an image file" --data-binary @"/home/user1/Desktop/test.jpg" localhost/uploader.php

The problem I am getting is as follows: 我遇到的问题如下:

Notice: Undefined index: image in /var/www/uploader.php

The problem is I am using $_FILES['image'] to pick up files in the PHP script. 问题是我正在使用$ _FILES ['image']在PHP脚本中拾取文件。

How do I adjust my cURL commands accordingly ? 如何相应地调整cURL命令?


#1楼

参考:https://stackoom.com/question/r9TJ/使用curl上传带有文件的POST数据


#2楼

You need to use the -F option: 您需要使用-F选项:
-F/--form <name=content> Specify HTTP multipart POST data (H)

Try this: 尝试这个:

curl \-F "userid=1" \-F "filecomment=This is an image file" \-F "image=@/home/user1/Desktop/test.jpg" \localhost/uploader.php

#3楼

Here is my solution, I have been reading a lot of posts and they were really helpful. 这是我的解决方案,我已经阅读了很多帖子,它们确实很有帮助。 Finally I wrote some code for small files, with cURL and PHP that I think its really useful. 最后,我用cURL和PHP编写了一些用于小文件的代码,我认为它确实很有用。

public function postFile()
{    $file_url = "test.txt";  //here is the file route, in this case is on same directory but you can set URL too like "http://examplewebsite.com/test.txt"$eol = "\r\n"; //default line-break for mime type$BOUNDARY = md5(time()); //random boundaryid, is a separator for each param on my post curl function$BODY=""; //init my curl body$BODY.= '--'.$BOUNDARY. $eol; //start param header$BODY .= 'Content-Disposition: form-data; name="sometext"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.$BODY .= "Some Data" . $eol;//param data in this case is a simple post data and 1 $eol for the end of the data$BODY.= '--'.$BOUNDARY. $eol; // start 2nd param,$BODY.= 'Content-Disposition: form-data; name="somefile"; filename="test.txt"'. $eol ; //first Content data for post file, remember you only put 1 when you are going to add more Contents, and 2 on the last, to close the Content Instance$BODY.= 'Content-Type: application/octet-stream' . $eol; //Same before row$BODY.= 'Content-Transfer-Encoding: base64' . $eol . $eol; // we put the last Content and 2 $eol,$BODY.= chunk_split(base64_encode(file_get_contents($file_url))) . $eol; // we write the Base64 File Content and the $eol to finish the data,$BODY.= '--'.$BOUNDARY .'--' . $eol. $eol; // we close the param and the post width "--" and 2 $eol at the end of our boundary header.$ch = curl_init(); //init curlcurl_setopt($ch, CURLOPT_HTTPHEADER, array('X_PARAM_TOKEN : 71e2cb8b-42b7-4bf0-b2e8-53fbd2f578f9' //custom header for my api validation you can get it from $_SERVER["HTTP_X_PARAM_TOKEN"] variable,"Content-Type: multipart/form-data; boundary=".$BOUNDARY) //setting our mime type for make it work on $_FILE variable);curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/1.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0'); //setting our user agentcurl_setopt($ch, CURLOPT_URL, "api.endpoint.post"); //setting our api post urlcurl_setopt($ch, CURLOPT_COOKIEJAR, $BOUNDARY.'.txt'); //saving cookies just in case we wantcurl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // call return contentcurl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); navigate the endpointcurl_setopt($ch, CURLOPT_POST, true); //set as postcurl_setopt($ch, CURLOPT_POSTFIELDS, $BODY); // set our $BODY $response = curl_exec($ch); // start curl navigationprint_r($response); //print response}

With this we should be get on the "api.endpoint.post" the following vars posted. 有了这个,我们应该在以下变量上找到“ api.endpoint.post”。 You can easily test with this script, and you should be receive this debugs on the function postFile() at the last row. 您可以轻松地使用此脚本进行测试,并且应该在最后一行的函数postFile()上收到此调试信息。

print_r($response); //print responsepublic function getPostFile()
{echo "\n\n_SERVER\n";echo "<pre>";print_r($_SERVER['HTTP_X_PARAM_TOKEN']);echo "/<pre>";echo "_POST\n";echo "<pre>";print_r($_POST['sometext']);echo "/<pre>";echo "_FILES\n";echo "<pre>";print_r($_FILEST['somefile']);echo "/<pre>";
}

It should work well, they may be better solutions but this works and is really helpful to understand how the Boundary and multipart/from-data mime works on PHP and cURL library. 它应该可以很好地工作,它们可能是更好的解决方案,但是它可以工作,并且对于理解Boundary和multipart / from-data mime如何在PHP和cURL库上工作非常有帮助。


#4楼

Catching the user id as path variable (recommended): 将用户ID捕获为路径变量(推荐):

curl -i -X POST -H "Content-Type: multipart/form-data"
-F "data=@test.mp3" http://mysuperserver/media/1234/upload/

Catching the user id as part of the form: 捕获用户ID作为表单的一部分:

curl -i -X POST -H "Content-Type: multipart/form-data"
-F "data=@test.mp3;userid=1234" http://mysuperserver/media/upload/

or: 要么:

curl -i -X POST -H "Content-Type: multipart/form-data"
-F "data=@test.mp3" -F "userid=1234" http://mysuperserver/media/upload/

#5楼

Here is how to correctly escape arbitrary filenames of uploaded files with bash : 这是使用bash正确转义上传文件的任意文件名的方法:

#!/bin/bash
set -euf="$1"
f=${f//\\/\\\\}
f=${f//\"/\\\"}
f=${f//;/\\;}curl --silent --form "uploaded=@\"$f\"" "$2"

#6楼

if you are uploading binary file such as csv, use below format to upload file 如果您要上传二进制文件(例如csv),请使用以下格式上传文件

curl -X POST \'http://localhost:8080/workers' \-H 'authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyIsInR5cGUiOiJhY2Nlc3MifQ.eyJ1c2VySWQiOjEsImFjY291bnRJZCI6MSwiaWF0IjoxNTExMzMwMzg5LCJleHAiOjE1MTM5MjIzODksImF1ZCI6Imh0dHBzOi8veW91cmRvbWFpbi5jb20iLCJpc3MiOiJmZWF0aGVycyIsInN1YiI6ImFub255bW91cyJ9.HWk7qJ0uK6SEi8qSeeB6-TGslDlZOTpG51U6kVi8nYc' \-H 'content-type: application/x-www-form-urlencoded' \--data-binary '@/home/limitless/Downloads/iRoute Masters - Workers.csv'

使用curl上传带有文件的POST数据相关推荐

  1. 服务器上传excel文件并读取数据,asp.net上传Excel文件并读取数据的实现方法

    前言 本文主要给大家介绍了关于asp.net上传Excel文件并读取数据的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 实现如下: 前台代码:使用服务端控件实现上传 服务端 ...

  2. java 导出文件上传模板,上传Excel文件批量导入数据

    后端代码 controller层 @RequestMapping(value = "/importExcel", method = {RequestMethod.POST, Req ...

  3. C++ vs2017 - libcurl - http请求 代码大全(请求数据,上传下载文件,多线程上传下载文件)

    在网上搜寻各种libcurl的用法,将代码集合于此! 目录 一.配置curl项目 二.Curl 请求参数 1. CURLOPT_POST 2. CURLOPT_URL 3. CURLOPT_HTTPH ...

  4. springboot实现上传Excel文件与数据库中的数据进行比对

    springboot实现上传Excel文件与数据库中的数据进行比对 首先先写好文件上传的接口,然后上传需要比对数据的文件,在点击数据比对 下面是一部分数据比对的代码: 后端controller部分: ...

  5. php curl上传文件返回false,php curl上传文件$_FILES为空的问题

    PHP 5.0~5.6 各版本兼容的cURL文件上传 最近做的一个需求,使用PHP cURL上传文件.踩坑若干,整理如下. 不同版本PHP之间cURL的区别 PHP的cURL支持通过给CURL_POS ...

  6. php curl上传文件$_FILES为空问题

    php使用curl上传文件,代码如下: 发送的代码(完全是官方的示例) <?php /* http://localhost/upload.php: print_r($_POST); print_ ...

  7. php 上传 413,PHP CURL上传文件出现413 Request Entity Too Large

    php在使用CURL上传文件时出现413 Request Entity Too Large,网上也查找了很多方案,但是都不起作用 经仔细检查,发现curl_setopt的各个参数设置的顺序也会有影响. ...

  8. php curl文件上传,在 php 中通过 CURL 上传文件

    1.使用 CURL 默认的方法$file = realpath('gif/1.gif'); //要上传的文件 $fields['f'] = '@'.$file; $ch = curl_init(); ...

  9. curl上传文件的命令

    curl是开源的http上传和下载工具,通过命令行实现http操作,也可以使用其源码进行http编程,就不用重新实现http协议的接口了. 网上有很多curl使用的命令行示例和基于其接口开发的示例,这 ...

最新文章

  1. Tornado自定义分布式session框架
  2. python找不到tushare_python tushare安装
  3. 未将对象引用设置到对象的实例
  4. java反射创建对象_java8反射创建对象
  5. 聊城大学计算机应用基础函授,聊城大学试题计算机应用基础试题
  6. 剖析 Apache 顶级项目 SkyWalking 的源码 ,看看它有什么好?
  7. SAP License:做系统要关注业务过程
  8. 使用 anacoda 安装scrapy
  9. LINUX使用gpg签名校验文件
  10. Android开发—基于OpenCV实现相机实时图像识别跟踪
  11. 编码应该运筹帷幄之中,决胜千里之外
  12. Relative Ranks问题及解法
  13. 《那些年啊,那些事——一个程序员的奋斗史》——123
  14. 最值得入手的五款骨传导耳机,几款高畅销的骨传导耳机
  15. Blender基础:融球(Metaball)
  16. Git 分支篇之远程分支
  17. 高炉的3D数字孪生体,线上操控高炉作业
  18. 3d打印,机器人,计算机,3D打印的机器人将教孩子计算机编码!
  19. 逆战小白提升日記——网红时钟罗盘北京时间页代码
  20. 一张图看懂SpringMVC

热门文章

  1. Prepare for Android
  2. Handler消息机制(七):Message的数据结构是什么样子
  3. 深入Java泛型(一):作用与定义
  4. 安卓高手之路之java层Binder
  5. 【脚下有根】之Skia库的matrix代码解读
  6. 算法------------ 最长公共前缀
  7. dubbo 配置文件详解
  8. studio添加依赖工程方法
  9. linux read函数_Linux中shell输入ls命令后会系统会发生什么
  10. scanner nextstring 空格_毫不起眼的小技巧,居然一分钟删除了数据中所有空格