gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:45:59 27 4
gpt4 key购买 nike

我正在尝试通过 curl-put 方法上传文档。但是当我调用这个文件时,它只生成 0 字节文件而不是上传文件。

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

这里缺少什么?标题中有什么吗?在文件名中我们需要传递文件名还是路径?

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

错误:

Unexpected end of MIME multipart stream. MIME multipart message is not complete.

最佳答案

试试这个:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("body"=> $BODY)));

或者至少对 $BODY 进行 URL 编码;

关于php - 使用 curl put 上传文件 - multipart/form-data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36790057/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com