gpt4 book ai didi

php - 通过php将数据请求发送到flask API

转载 作者:行者123 更新时间:2023-12-03 16:58:40 24 4
gpt4 key购买 nike

我正在尝试通过自制的Flask API发送数据。
这是我的api代码:

def add_message(uuid):
content = request.json
try:
print(content['mytext'])
except:print(content)
return jsonify({"uuid":uuid})
if __name__ == '__main__':
app.run(debug=True)


如果我通过python发送数据,则一切正常:

res = requests.get('https://**.pythonanywhere.com/123', json={"mytext":"test"})
if res.ok:
print( res.json())


但是,如果我使用php:

$host = "https://**.pythonanywhere.com";

$url = $host.'/1237R4';

echo http($url,json_encode([
"mytext"=>"python rules"
]),'post');
function http($url,$data=[],$method='get'){
$ch = curl_init();
$chOpts = [
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_HEADER=>false,
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_CONNECTTIMEOUT =>8,
CURLOPT_TIMEOUT => 16,
CURLOPT_HTTPHEADER,[
'Content-Type: application/json'
]
];
if($method=='post'){
$chOpts[CURLOPT_POST]=true;
$chOpts[CURLOPT_POSTFIELDS]=$data;
$chOpts[CURLOPT_URL]=$url;
}
else{
$url.='?'.is_array($data)?http_build_query($data):$data;
$chOpts[CURLOPT_URL]=$url;
}
echo 'Request: '.$method.'['.$url.']'."\n";
print_r($data);
curl_setopt_array($ch, $chOpts);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}


我在日志中收到以下错误:

[b'{"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}']


如何使其适用于php?

最佳答案

$req = $http({method: 'get',url: "https://**.pythonanywhere.com/123",params: {mytext: "test"});


现在 mytext变成了字符串,您没有将它作为字符串传递。

关于php - 通过php将数据请求发送到flask API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48811905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com