Nguyen Duc Tien
Nguyen Duc Tien

Reputation: 31

PHP can't call API from FastAPI

I built an API with FastAPI, then I want to call it by PHP (I ran PHP by Docker, expose port 80 to 80), but It give always boolean(False). However this API works very well with JavaScript, Postman, Firefox.(I want to give results from this API to externals users so my ideal is using PHP to bring the results from this API, then give it to Front-end, I don't know how to give this API directly from FastAPI to externals users). So you can see my code for FastAPI below:

    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

And here is code in 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);
    ?>

I also tried with file_get_contents but not thing changed.

Thank you in advance!

Upvotes: 1

Views: 2270

Answers (1)

Nguyen Duc Tien
Nguyen Duc Tien

Reputation: 31

I just found the solution, curl on php on docker can't call the api from host with ip 127.0.0.1. I use command ip addr show docker0 then I take the inet address instead of localhost, and It worked.

Upvotes: 2

Related Questions