Python PHP可以';t从FastAPI调用API

Python PHP可以';t从FastAPI调用API,python,php,api,rest,fastapi,Python,Php,Api,Rest,Fastapi,我用FastAPI构建了一个API,然后我想用PHP调用它(我用Docker运行PHP,暴露端口80到80),但它总是给出布尔值(False)。 然而,这个API在JavaScript、Postman、Firefox上运行得非常好(我想将这个API的结果提供给外部用户,所以我的理想是使用PHP将这个API的结果提供给前端,我不知道如何将这个API直接从FastAPI提供给外部用户)。 因此,您可以在下面看到我的FastAPI代码: from fastapi import FastAPI

我用FastAPI构建了一个API,然后我想用PHP调用它(我用Docker运行PHP,暴露端口80到80),但它总是给出布尔值(False)。 然而,这个API在JavaScript、Postman、Firefox上运行得非常好(我想将这个API的结果提供给外部用户,所以我的理想是使用PHP将这个API的结果提供给前端,我不知道如何将这个API直接从FastAPI提供给外部用户)。 因此,您可以在下面看到我的FastAPI代码:

    from fastapi import FastAPI #import class FastAPI() from library fastapi
    from pydantic import BaseModel
    import main_user
    import user_database
    from fastapi.middleware.cors import CORSMiddleware
    app = FastAPI() #constructor and  app

    origins = [
       "http://localhost",
       "http://localhost:80",
       "https://localhost",
       "https://localhost:80"
   ]
   app.add_middleware(
       CORSMiddleware,
       allow_origins=['*'],
       allow_credentials=True,
       allow_methods=["*"],
       allow_headers=["*"],
   )

   class InfosCalculIndice(BaseModel):
       nomTuile:str
       dateAcquisition:str
       heureAcquisition:str
       nomIndice:str
       lonMin: float
       lonMax: float
       latMin: float
       latMax: float
       interp: str
       imgFormat: str
   class InfosLogin(BaseModel):
       username: str
       password: str
   class InfosTuile(BaseModel):
       tuile:str

   @app.post("/calcul-indice")
   async def calcul_indice(infos: InfosCalculIndice):
       img = main_user.calcul_api(infos.nomTuile,                                                                                            infos.dateAcquisition,infos.nomIndice,infos.lonMin,
       infos.lonMax,infos.latMin,infos.latMax,infos.interp, infos.imgFormat)
       return {"img":img}
   @app.post("/login")
   async def login(infos: InfosLogin):
       status = user_database.login(infos.username, infos.password)
       return {"stt":status}
   @app.post("/register")
   async def register(infos: InfosLogin):
       stt = user_database.createUser(infos.username, infos.password)
       return {"stt":stt}
   @app.get("/get-tuile")
   async def getTuile():
       tuiles = user_database.getAllTuiles()
       return tuiles
下面是PHP中的代码:

    <?php

    $url = 'http://localhost/login'; #OR http://127.0.0.1:800/login
    $data = array("username" => "myusernam", "password"=>"mypassword");
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS,  json_encode($data));
    curl_setopt($curl, CURLOPT_PORT, 8000); #OR without this option
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // For HTTPS
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // For HTTPS
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
   'Content-Type: application/json', 'server:uvicorn'
    ]);
    $response = curl_exec($curl);
    echo $response;
    var_dump($response);
    curl_close($curl);
    ?>

我还尝试了
文件获取内容
,但没有改变


提前谢谢你

我刚刚找到了解决方案,docker上php上的curl无法从ip 127.0.0.1的主机调用api。
我使用命令
ip addr show docker0
然后我使用inet地址而不是localhost,它成功了。

运行PHP时是否收到错误消息?您在FastAPI中是否收到任何错误消息?您是否在日志文件中写入任何内容以查看得到的结果?你们甚至可以用烧瓶来建造前端。或者使用JavaScript的纯HTML(直接或使用ie.React、Vue)。据我所知,暴露端口用于从外部访问PHP,而不是从PHP访问外部。我不确定docker是否使用
localhost
作为IP。它可能使用子网
172.17.0.0/16
Thank you@furas中的某些内容,根据您的建议,我找到了解决方案。PHP on docker容器无法访问主机端口。