【问题标题】:Get a file from user in php and send it to a flask server without saving it在 php 中从用户获取文件并将其发送到烧瓶服务器而不保存它
【发布时间】:2019-07-06 08:10:05
【问题描述】:

我正在尝试通过 php curl 将文件发送到烧瓶服务器我在这里做错了什么。

这是场景: - 用户访问我的 php 应用程序并上传一个 excel 文件 - 我的应用程序通过 php_curl 将文件发送到 (python) 烧瓶应用程序

php代码==========================

public function actiongetFileSendIt(){
    if (function_exists('curl_file_create')) { 
    $cFile = curl_file_create($_FILES['file']['tmp_name'],$_FILES['file']['type'],'file');
    } else {
       $cFile = '@' . realpath($_FILES['file']['tmp_name'].';filename='.$_FILES['file']['name'].';type='.$_FILES['file']['type']);
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://0.0.0.0:8000/api/pos/import");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $cFile);
    $result = curl_exec($ch);
    curl_close($ch);
    echo "<pre>";print_r($result);echo "</pre>";exit("Ends Here");
}

python代码========================

if 'file' not in request.files: // php request fails here
    current_app.logger.info(f"request.files 1 {request.files}")
    current_app.logger.error('File not found')
    return resp
file = request.files['file']
if not file.filename:
    current_app.logger.info(f"request.files 2 {request.files}")
    current_app.logger.error('Invalid file type found')
    return resp
if file:
    current_app.logger.info(f"request.files 3 {request.files}")

============== LOGS =============

工作卷发帖

curl -X POST   http://0.0.0.0:8000/api/pos/import   -H 'cache-control: no-cache'   -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'   -H 'postman-token: 73b974f1-3e3f-713d-2a34-a1abeb3d9a80'   -F 'file=@/home/anubavam/Downloads/Corrected_Regulations MA YOGA.xlsx'
{
  "success": "Plan of study import completed"
}

python日志

request.files 3 ImmutableMultiDict([('file', <FileStorage: 'Corrected_Regulations MA YOGA.xlsx' ('application/octet-stream')>)])

php curl post的python日志不工作

request.files 1 ImmutableMultiDict([])

问题 1:如何在 php $_FILES 中从用户那里获取文件并将其转换为 curl 帖子。 问题 2:如何使用 {'file':/path/or/whatever/from/question1}

之类的键值对将 php curl 发布到烧瓶

【问题讨论】:

    标签: php python-3.x curl post flask


    【解决方案1】:

    我找到了解决方案。如果有人想处理这种情况

    if (function_exists('curl_file_create')) { 
                            $cFile = curl_file_create($_FILES['file']['tmp_name']);
                          } else {
                            $cFile = '@' . realpath($_FILES['file']['tmp_name']);
                        }
                        $post = array('file'=> $cFile);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL,"http://0.0.0.0:8000/api/pos/import");
                        curl_setopt($ch, CURLOPT_POST,1);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
                        $result=curl_exec ($ch);
                        curl_close ($ch);
    

    【讨论】:

      猜你喜欢
      • 2020-06-23
      • 2018-08-30
      • 1970-01-01
      • 2021-02-03
      • 2021-11-21
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多