i have two backends made of php and python fastAPI. i have to make them communicate each other.
python server
from fastapi import FastAPI, File, UploadFile, Path, Request, Form
from fastapi.responses import HTMLResponse
from typing import List, Dict, Optional
from pydantic import BaseModel
import numpy as np
import cv2
from PIL import Image
import io
import joblib
app = FastAPI(title="skin oil ML API",
description="API for skin oil dataset ml model", version="1.0")
@app.get("/predict/skin/oil")
async def main():
content = """
<body>
<div>
<h6>muliple</h6>
<form action="/predict/skin/oil" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
<h6>same name</h6>
<form action="/predict/skin/oil" enctype="multipart/form-data" method="post">
<input name="files" type="file" >
<input name="files" type="file" >
<input type="submit">
</form>
</div>
</body>
"""
return HTMLResponse(content=content)
@app.post("/predict/skin/oil")
async def predict_skin_oil(files: List[UploadFile] = File(...)):
resArr = [];
for file in files:
contents = await file.read()
npArr = np.fromstring(contents, np.uint8)
img = cv2.imdecode(npArr, cv2.IMREAD_GRAYSCALE)
img = np.array(cv2.resize(img, dsize=(256, 256),
interpolation=cv2.INTER_AREA))
img = img.reshape(-1, 256*256)
global model
resArr.append(model.predict(img)[0])
return {"result": resArr}
with that html form above for test, it works fine.
but i can't implement same thing with in php Curl.
here is my php code
$data = array('files'=>curl_file_create("../img/img1.jpg",'image/jpg', basename("../img/img1.jpg")),
'files'=>curl_file_create("../img/img2.jpg",'image/jpg', basename("../img/img2.jpg")),
'files'=>curl_file_create("../img/img3.jpg",'image/jpg', basename("../img/img3.jpg")));
$other_data_tried = array('files[0]'=>curl_file_create("../img/img1.jpg",'image/jpg', basename("../img/img1.jpg")),
'files[1]'=>curl_file_create("../img/img2.jpg",'image/jpg', basename("../img/img2.jpg")),
'files[2]'=>curl_file_create("../img/img3.jpg",'image/jpg', basename("../img/img3.jpg")));
$url="http://myserver/predict/skin/oil";
$headers = array("Content-Type:multipart/form-data");
$curl = curl_init();
curl_setopt_array(
$curl, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST=> true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
)
);
$response = curl_exec($curl);
$res = [
"res" => $response
];
$err = curl_error($curl);
if ($err) {
echo 'cURL Error #:' . $err;
} else {
echo json_encode($res);
}
when i try to post multiple files in a same key "files" i got only one object in python server. i know php associated array not work that way. Uploading multiple files in PHP using HTML Form or cURL so i tried 'files[0]'. it gives me this error.
422 Unprocessable Entity\r\ndate: Wed, 15 Sep 2021 04:27:36 GMT\r\nserver: uvicorn\r\ncontent-length: 89\r\ncontent-type: application\/json\r\n\r\n{\"detail\":[{\"loc\":[\"body\",\"files\"],\"msg\":\"field required\",\"type\":\"value_error.missing\"}]}",
how can i make php curl work same as test html form like below...
<form action="/predict/skin/oil" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
<h6>same name</h6>
<form action="/predict/skin/oil" enctype="multipart/form-data" method="post">
<input name="files" type="file" >
<input name="files" type="file" >
<input type="submit">
</form>
curl -X 'POST' \
'http://127.0.0.1:8000/predict/skin/oil' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'files=@Face2_Right_001.jpg;type=image/jpeg' \
-F 'files=@Face2_Right_005.jpg;type=image/jpeg' \
-F 'files=@Face2_Left_002.jpg;type=image/jpeg'
this kind of thing in php curl !! ㅠㅠ
$data['files']an array?