dongmaobeng7145 2016-02-12 12:08
浏览 74

如何在PHP中使用curl -T / - upload-file和POST?

I'm implementing an integration to an API and according to example in the documentation I need to post a file using -T/--upload-file.

curl -u "username:password" -0 –X POST -T filename.txt -H "Content-Type: text/plain" "http://url"

My solution:

$fp = fopen('/home/myFile.txt', 'rb');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize('/home/myFile.txt'));

The problem however is that CURLOPT_INLINE only works when using PUT, whereas the example is using POST.

I've seen the following solution but I don't think that's what I'm supposed to use since it doesn't seem to be the same thing as -T. I don't see any mention of key name in the documentation either.

curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, array('file'=>'@/home/myFile.pdf'));

Is there a way to do this properly?

  • 写回答



      相关推荐 更多相似问题


      • ¥15 实值特征权重向量和特征选择,多分类
      • ¥15 INVEST年产水量模块结果出处理
      • ¥15 Neo4j 不显示关系
      • ¥15 这段代码有什么bug
      • ¥15 pip默认安装位置修改不成功
      • ¥15 C++ inline、staitc、const三合一谁能讲通透
      • ¥15 关于pyqt5中QThread的问题
      • ¥20 STM32基于HAL库使用串口发送16进制数组
      • ¥15 OpenCV报错 You can enable it via 'OPENCV_IO_ENABLE_OPENEXR'
      • ¥15 AT89C51的程序问题