PHP-python脚本中的CMD Curl等价物

PHP-python脚本中的CMD Curl等价物,php,curl,Php,Curl,我正在通过CMD调用pythonapi @app.route("/getResult", methods = ['GET', 'POST']) def funcName(): print("Fetching the passed input file") f = request.files['data'] print("Reading the file using pandas") data = pd

我正在通过CMD调用pythonapi

@app.route("/getResult", methods = ['GET', 'POST'])
def funcName():
    print("Fetching  the passed input file")
    f = request.files['data']

    print("Reading the file using pandas")
    data = pd.read_csv(f)
这就是我在cmd中调用它的方式,它只返回一个数字输出

curl -F data=@location\data_sample.csv localhost:5000/getResult
我试图用下面的代码通过php调用它

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $uri);
        $postData = array(
            'data' => '@'.$file_path,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $response = curl_exec($ch);
        print_r($response);die();
我得到这个错误:

werkzeug.exceptions.BadRequestKeyError

werkzeug.exceptions.BadRequestKeyError:400错误请求:浏览器(或代理)发送了此服务器无法理解的请求。KeyError:“数据”


至于我,您只发送字符串
@location\data\u sample.csv
,因为命令
@
可能只在real
curl
中起作用

根据PHP文档,您应该使用或创建
curl\u文件

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $uri);

// Create a CURLFile object
$cfile = curl_file_create('location/data_sample.csv', 'text/csv', 'data_sample.csv');

 // Assign POST data
$data = array('data' => $cfile);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);
print_r($response);die();

编辑:

我用来测试它的完整代码

main.py

from flask import Flask
import pandas as pd

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    print("Fetching  the passed input file")
    f = request.files['data']

    print("Reading the file using pandas")
    data = pd.read_csv(f)
    
    print("Send back as HTML with table")
    return data.to_html()

if __name__ == '__main__':
    #app.debug = True
    app.run() #debug=True 
main.php

<?php

$uri = 'http://127.0.0.1:5000/';

$file_path = 'location/data_sample.csv';

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $uri);

//$postData = array('data' => '@'.$file_path);  // doesn't work

// Create a CURLFile object
$cfile = curl_file_create($file_path, 'text/csv', 'data_sample.csv');
//$cfile = new CURLFile($file_path, 'text/csv', 'data_sample.csv');

 // Assign POST data
$postData = array('data' => $cfile);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

$response = curl_exec($ch);

print_r($response);
die();

?>