我如何配置颤动使用两个不同的API?
我有一个Flutter应用程序,我想调用使用不同数据库的多个API端点。
我不知道我应该在后端还是前端进行此操作。
api1.php:
<?php // header('Content-Type: application/json');
include "config.php";$sql = "select inventeries.pic_inv,inventeries.name,site.Sname,users.number from inventeries,site,users";
$stmt = $con->prepare($sql);
$stmt->execute();
$med = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($med);
?>
api_2.php:
<?php
// header('Content-Type: application/json');
include "config.php";
$sql = "select users.pic_inv,users.name,site.Sname,users.number from users,site,users";
$stmt = $con->prepare($sql);
$stmt->execute();
$med = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($med);
?>
当前它们使用相同的数据库,因此两个API的结果相同:
{"pic_inv":"ACICAL-PLUS.jpg","name":"Aclcal","Sname":"flowers pharma","number":"7894561231"}
{"pic_inv":"ACICAL-PLUS.jpg","name":"Aclcal","Sname":"flowers pharma","number":"123456789"}
{"pic_inv":"what-a-treatment-for-migraine-headache.jpg","name":"foar","Sname":"flowers pharma","number":"7894561231"}
{"pic_inv":"what-a-treatment-for-migraine-headache.jpg","name":"foar","Sname":"flowers pharma","number":"123456789"}
{"pic_inv":"syrunj.png","name":"AMLO","Sname":"flowers pharma","number":"7894561231"}
{"pic_inv":"syrunj.png","name":"AMLO","Sname":"flowers pharma","number":"123456789"}
{"pic_inv":"cap.jpg","name":"pandol","Sname":"flowers pharma","number":"7894561231"}
{"pic_inv":"cap.jpg","name":"pandol","Sname":"flowers pharma","number":"123456789"}
I have a Flutter app and I would like to call multiple API endpoints which use different databases.
I don't know if I should do this on the backend or the frontend.
api1.php:
<?php // header('Content-Type: application/json');
include "config.php";$sql = "select inventeries.pic_inv,inventeries.name,site.Sname,users.number from inventeries,site,users";
$stmt = $con->prepare($sql);
$stmt->execute();
$med = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($med);
?>
api_2.php:
<?php
// header('Content-Type: application/json');
include "config.php";
$sql = "select users.pic_inv,users.name,site.Sname,users.number from users,site,users";
$stmt = $con->prepare($sql);
$stmt->execute();
$med = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($med);
?>
Currently they use the same database, so the results from both APIs are the same:
{"pic_inv":"ACICAL-PLUS.jpg","name":"Aclcal","Sname":"flowers pharma","number":"7894561231"}
{"pic_inv":"ACICAL-PLUS.jpg","name":"Aclcal","Sname":"flowers pharma","number":"123456789"}
{"pic_inv":"what-a-treatment-for-migraine-headache.jpg","name":"foar","Sname":"flowers pharma","number":"7894561231"}
{"pic_inv":"what-a-treatment-for-migraine-headache.jpg","name":"foar","Sname":"flowers pharma","number":"123456789"}
{"pic_inv":"syrunj.png","name":"AMLO","Sname":"flowers pharma","number":"7894561231"}
{"pic_inv":"syrunj.png","name":"AMLO","Sname":"flowers pharma","number":"123456789"}
{"pic_inv":"cap.jpg","name":"pandol","Sname":"flowers pharma","number":"7894561231"}
{"pic_inv":"cap.jpg","name":"pandol","Sname":"flowers pharma","number":"123456789"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,正如我已经说过的,我没有PHP的经验,并且在API中都会更容易。
但是您可以在扑朔迷离的应用中制作一个模型,例如您从 db1 endpoint中获得的数据 是这样的
,您从 db2端点获得的数据就是这样
,然后您准备了一个将所有内容组合在一起的模型。.让我们称其为 mixchmodel
您可以使用 https://app.quicktype.io/ 生成数据的模型。
然后,您可以制作一个可以调用两个端点的函数
我正在使用 dio package 进行API呼叫。
我希望您得到这个想法
Well, as i have said, i have no experience with PHP and all would be easier in the API..
but you can make a model in your flutter app like lets say the data you get from DB1 endpoint is like this
and the data you get from DB2 Endpoint is like this
then you prepare a model that combines all to be something like this.. lets call it MixedModel
you can use https://app.quicktype.io/ to generate the models for your data.
then you can make a function that can make a call to both of the endpoints
im using Dio Package to make the API Calls..
I hope you get the idea