4

我正在尝试使用 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。

4

2 回答 2

0

诀窍是将二进制字符串变成内存中的,然后告诉 curl 上传该流,使用:CURLOPT_INFILE、CURLOPT_INFILESIZE 和 CURLOPT_UPLOAD。该答案提供了执行此操作的代码。

于 2015-12-11T13:15:48.100 回答
0

与处理其他变量的方式完全相同。最后,一切都以“二进制”形式发送,或者实际上以字符串表示形式发送。

所以就这样做

$content = ob_get_contents();
//or file_get_contents() or whatever source
$data_upload = array('username' => $username, 
                 'password' => $password, 
                 'owner' => $owner, 
                 'destination' => $pathpda, 
                 'filename' => $filename, 
                 'filedata' => $cfile,
                 'allkindsofdata' => $content );
于 2015-11-03T12:36:30.057 回答