PHP 无法从 FastAPI 调用 API

PHP can't call API from FastAPI

我用FastAPI建了一个API,然后我想用PHP调用它(我运行PHP用Docker,将端口 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);
    ?>

我也尝试过 file_get_contents 但没有任何改变。

提前致谢!

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