我正在尝试使用 cURL 将原始二进制文件与其他一些数据一起发送到 POST 中。基本上,我是这样做的:
//Here i create the CURLFile
$cfile = new CURLFile($fullpath,'application/xml', $filename);
//Data to upload (including the file)
$data_upload = array('username' => $username,
'password' => $password,
'owner' => $owner,
'destination' => $pathpda,
'filename' => $filename,
'filedata' => $cfile);
//Upload
$ch_upload = curl_init($urlws.$e_upload);
curl_setopt($ch_upload, CURLOPT_POST,true);
curl_setopt($ch_upload, CURLOPT_POSTFIELDS, $data_upload);
curl_setopt($ch_upload, CURLOPT_RETURNTRANSFER, true);
$response_upload = curl_exec($ch_upload);
curl_close($ch_upload);
一切正常。除了现在我希望我的文件只是二进制数据而不是磁盘上的真实文件。另外,我需要在请求中发送其他数据!有谁知道该怎么做?我在 Google 中查找,但我无法找到同时发送原始二进制文件和其他数据的方法。谢谢!
更新为了进一步澄清我的问题,我不需要只发送随机二进制数据,而是发送内容来自 ob_get_contents() 的文件(例如),因此它不是磁盘上的物理文件。如果我只是替换 CURLfile 以使用“filedata”变量发送二进制文件数据,它根本不起作用。Web 服务无法识别该文件。也许有一种方法可以在不指向真实文件的情况下创建 CURLfile。