php curl put file,用curl上传文件put - multipart/form-data

我想通过curl-put方法上传文件。但是当我调用这个文件时,它只是生成0字节的文件而不是上传文件。用curl上传文件put - multipart/form-data

我在使用第三方的网络服务。

这里缺少什么?标题中是否有内容? 在文件名中,我们是否需要传递文件名或路径?

PHP代码

$data = json_encode($this->data);

$file_url = $filepath;

$f = array($filepath);

$eol = "\r\n";

$BOUNDARY = md5(time());

$BODY=""; //init my curl body

$BODY.= '--'.$BOUNDARY. $eol; //start param header

$BODY .= 'Content-Disposition: form-data; name="Appointment"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.

$BODY.= '--'.$BOUNDARY. $eol; // start 2nd param,

$BODY.= 'Content-Disposition: form-data; filename='.$filepath. $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/pdf' . $eol; //Same before row

$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.

$header[] = 'Authorization: WRAP access_token="'.$this->accesstoken.'"';

$header[] = 'Content-Type: multipart/form-data; boundary='.$BOUNDARY;

print_r($BODY);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$this->baseAddress);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // tell curl you want to post something

curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY); // define what you want to post

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER,$header);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_ENCODING, '');

curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1);

$output = curl_exec ($ch); // execute

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

$header = substr($output, 0, $header_size);

$this->response = substr($output, $header_size);

$info = curl_getinfo($ch);

$this->responsecode = $info['http_code'];

curl_close ($ch); // close curl

我也试图通过创建对象发送文件。在这种情况下,它给出了以下错误。

$cfile = new CURLFile(realpath('testcp.txt'),'text/plain');

$uploadPost = array (

'file' => $cfile

);

$eol = "\r\n";

$BOUNDARY = md5(time());

$BODY=""; //init my curl body

$BODY.= '--'.$BOUNDARY. $eol; //start param header

$BODY.= '--'.$BOUNDARY. $eol; // start 2nd param,

$BODY.= '--'.$BOUNDARY .'--' . $eol. $eol; // we close the param and the post width "--" and 2 $eol at the end of our boundary header.

$header[] = 'Authorization: WRAP access_token="'.$this->accesstoken.'"';

$header[] = 'Content-Type: multipart/form-data; boundary='.$BOUNDARY;

print_r($BODY);

//ECHO $this->accesstoken;EXIT;

$ch = curl_init();

//echo $this->baseAddress;exit;

curl_setopt($ch, CURLOPT_URL,$this->baseAddress);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // tell curl you want to post something

// curl_setopt($ch, CURLOPT_POSTFIELDS, ($data)); // define what you want to post

curl_setopt($ch, CURLOPT_POSTFIELDS, $uploadPost); // define what you want to post

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER,$header);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_ENCODING, '');

curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1);

$output = curl_exec ($ch); // execute

echo 'OUTPUT ---';print_r($output);exit;

curl_close ($ch);

错误:

MIME多流的意外结束。 MIME多部分消息不是 完整。

2016-04-22

Aanshi

+2

严重的是,不要打扰卷曲。获取[Composer](http://getcomposer.org)并使用[Guzzle](http://docs.guzzlephp.org/en/latest/quickstart.html#uploading-data) - 否则使用guzzle。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值