curl 表单数据
cURL is the magical utility that allows developers to download a URL's content, explore response headers, get stock quotes, confirm our GZip encoding is working, and much more. One more great usage of cUrl for command line is POSTing form data to a server, especially while testing moderate to advanced form processing. And just like other cURL commands, POSTing form data is incredibly simple.
cURL是一种神奇的实用程序,它使开发人员可以下载URL的内容 , 浏览响应标题 , 获取股票报价 , 确认我们的GZip编码正常运行等等。 命令行使用cUrl的另一大用途是将表单数据发布到服务器,特别是在测试中级到高级表单处理时。 就像其他cURL命令一样,POST表单数据非常简单。
使用cURL过帐表单数据 (POSTing Form Data with cURL)
Start your cURL command with curl -X POST and then add -F for every field=value you want to add to the POST:
使用curl -X POST启动cURL命令,然后为要添加到POST的每个field=value添加-F :
curl -X POST -F 'username=davidwalsh' -F 'password=something' http://domain.tld/post-to-me.php
If you were using PHP, you could use print_r on the $_POST variable to see that your server received the POST data as expected:
如果您使用的是PHP,则可以在$_POST变量上使用print_r来查看服务器是否按预期接收到POST数据:
Array(
'username' => 'davidwalsh',
'password' => 'something'
)
If you need to send a specific data type or header with cURL, use -H to add a header:
如果需要使用cURL发送特定的数据类型或标头,请使用-H添加标头:
# -d to send raw data
curl -X POST -H 'Content-Type: application/json' -d '{"username":"davidwalsh","password":"something"}' http://domain.tld/login
使用cURL发布文件 (POSTing Files with cURL)
POSTing a file with cURL is slightly different in that you need to add an @ before the file location, after the field name:
使用cURL张贴文件稍有不同,因为您需要在文件位置之前,字段名称之后添加@ :
curl -X POST -F 'image=@/path/to/pictures/picture.jpg' http://domain.tld/upload
Using PHP to explore the $_FILES variable array would show file data as though it was uploaded via a form in browser:
使用PHP探索$_FILES变量数组将显示文件数据,就像通过浏览器中的表单上传文件一样:
Array(
"image": array(
"name" => "picture.jpg"
"type" => "image/jpeg",
"tmp_name" => "/path/on/server/to/tmp/phprj5rkG",
"error" => 0,
"size" => 174476
)
)
POSTing file contents with cURL is Probably easier than you thought, right?
使用cURL发布文件内容可能比您想象的要容易,对吧?
The first time I needed to POST file data from command line I thought I was in for a fight; instead I found that cURL made the process easy!
第一次需要从命令行发布文件数据时,我以为自己会参加战斗。 相反,我发现cURL使该过程变得容易!
curl 表单数据
本文介绍了如何使用cURL工具进行POST表单数据提交,包括基础的表单数据POST方法以及如何通过cURL上传文件。通过示例展示了在PHP中验证接收的POST数据,并解释了如何添加特定的HTTP头。cURL使得在命令行中处理这些任务变得简单易行。
333

被折叠的 条评论
为什么被折叠?



