简体   繁体   English

使用mongo gridfs从文件资源中创建curl_file_create

[英]curl_file_create from file resource using mongo gridfs

I have files inside MongoDb stored as binary data, I can get the binary data and if I var_dump it it'll show it as Resource #1000 or similar. 我在MongoDb中将文件存储为二进制数据,我可以获取二进制数据,如果我使用var_dump ,它将显示为Resource #1000或类似名称。 Now I need to send this file via curl to other server for processing, however curl_file_create wants file path. 现在,我需要通过curl将这个文件发送到其他服务器进行处理,但是curl_file_create文件路径。

What should I do if my file has no path? 如果我的文件没有路径该怎么办?

I have tried putting it inside like so but with no success. 我试图像这样将其放入内部,但没有成功。

private function sendToApi($resource, $name) {
    $cFile = curl_file_create($resource, 'application/pdf', $name);
    $postData = ['file' => $cFile];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERPWD, 'username' . ':' . 'pass');
    curl_setopt($ch, CURLOPT_URL, 'http://api.endpoint.com/process/pdf');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    $res = curl_exec($ch);
    curl_close($ch);

    $obj = json_decode($res);
    return $obj;
}

Error 错误

PHP Warning: curl_file_create() expects parameter 1 to be string, resource given. PHP警告:curl_file_create()期望参数1为字符串,资源已给定。

要获取gridFS二进制数据,请使用方法“ getBytes()” http://php.net/manual/en/mongogridfsfile.getbytes.php

$cFile = curl_file_create($resource->getBytes(), 'application/pdf', $name);

Create file as temp file and use it. 将文件创建为临时文件并使用。

$tmp = tmpfile();
fwrite($tmp, $content);
fseek($tmp, 0);
$meta = stream_get_meta_data($tmp);
$cFile = curl_file_create($meta['uri'], 'application/pdf', $name);
$post = ['file' => $cFile];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM