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 withfile_get_contents
but not thing changed.
Thank you in advance!
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 commandip addr show docker0
then I take the inet address instead of localhost, and It worked.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.