基于php构建APi流程,php – 如何构建一个RESTful API?
这里是一个非常简单的例子在简单的php。
有2个文件client.php& api.php。我把这两个文件放在同一个url:http:// localhost:8888 /,所以你必须更改链接到你自己的网址。 (文件可以在两个不同的服务器上)。
这只是一个例子,它非常快速和肮脏,加上它已经很长时间,因为我做了php。但这是一个api的想法。
client.php
/*** this is the client ***/
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_user") // if the get parameter action is get_user and if the id is set, call the api to get the user information
{
$user_info = file_get_contents('http://localhost:8888/api.php?action=get_user&id=' . $_GET["id"]);
$user_info = json_decode($user_info, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
Name: | <?php echo $user_info["last_name"] ?> |
First Name: | <?php echo $user_info["first_name"] ?> |
Age: | <?php echo $user_info["age"] ?> |
Return to the user list
}
else // else take the user list
{
$user_list = file_get_contents('http://localhost:8888/api.php?action=get_user_list');
$user_list = json_decode($user_list, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
alt=<?php echo "user_" . $user_["id"] ?>><?php echo $user["name"] ?>
}
?>
api.php
// This is the API to possibility show the user list, and show a specific user by action.
function get_user_by_id($id)
{
$user_info = array();
// make a call in db.
switch ($id){
case 1:
$user_info = array("first_name" => "Marc", "last_name" => "Simon", "age" => 21); // let's say first_name, last_name, age
break;
case 2:
$user_info = array("first_name" => "Frederic", "last_name" => "Zannetie", "age" => 24);
break;
case 3:
$user_info = array("first_name" => "Laure", "last_name" => "Carbonnel", "age" => 45);
break;
}
return $user_info;
}
function get_user_list()
{
$user_list = array(array("id" => 1, "name" => "Simon"), array("id" => 2, "name" => "Zannetie"), array("id" => 3, "name" => "Carbonnel")); // call in db, here I make a list of 3 users.
return $user_list;
}
$possible_url = array("get_user_list", "get_user");
$value = "An error has occurred";
if (isset($_GET["action"]) && in_array($_GET["action"], $possible_url))
{
switch ($_GET["action"])
{
case "get_user_list":
$value = get_user_list();
break;
case "get_user":
if (isset($_GET["id"]))
$value = get_user_by_id($_GET["id"]);
else
$value = "Missing argument";
break;
}
}
exit(json_encode($value));
?>
我没有对这个例子的任何调用数据库,但通常这是你应该做的。您还应该用“curl”替换“file_get_contents”函数。
基于php构建APi流程,php – 如何构建一个RESTful API?相关推荐
- python实现简单的api接口-用python写一个restful API
# -*- coding: utf-8 -*- # 作者: 煮酒品茶 """ package.module ~~~~~~~~~~~~~~ python实现的图书的一个re ...
- 使用python的Flask实现一个RESTful API服务器端
使用python的Flask实现一个RESTful API服务器端[翻译] 最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了 ...
- python restful api_用Python语言写一个restful API
本文主要向大家介绍了用Python语言写一个restful API,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助. """ package.modul ...
- Restful API是什么?初探Restful API,传统接口写法与Restful API接口写法区别
Restful API是什么?初探Restful API?为什么要用Restful API?传统接口写法与Restful API接口写法区别,带着这些问题我们来具体了解下Restful API: 目录 ...
- 使用 SpringBoot 构建一个RESTful API
文章目录 背景 创建 SpringBoot 项目/模块 SpringBoot pom.xml api pom.xml 创建 RESTful API 应用 @SpringBootApplication ...
- php slim 教程,Slim - 超轻量级PHP Restful API构建框架
下载源码包: http://www.slimframework.com/ 基于Slim的Restful API Sample: require '/darjuan/Slim/Slim.php'; us ...
- 通过亚马逊云科技实现基于 Restful API 的 CloudFront Distribution 复制/克隆功能
背景 Amazon CloudFront 是一个全球性的内容分发网络 (CDN),您可以借助 CloudFront 以低延迟和高可用性向查看者或者最终用户分发内容.通常来讲,Amazon CloudF ...
- java鉴权_一个开箱即用的高效认证鉴权框架,专注于restful api的认证鉴权动态保护...
作者:tomsun28 来源:SegmentFault 思否 写在开头 看了看这个专栏的最近一篇文章已经是两年前了,时间过得好快.应该是出学校后时间就很快了.两年前因为用shiro后,自己就按着想法开 ...
- 人人都是 API 设计者:我对 RESTful API、GraphQL、RPC API 的思考
有一段时间没怎么写文章了,今天提笔写一篇自己对 API 设计的思考.首先,为什么写这个话题呢?其一,我阅读了<阿里研究员谷朴:API 设计最佳实践的思考>一文后受益良多,前两天并转载了这篇 ...
最新文章
- springboot 问题总结
- 直播实录 | AlphaGo Zero是如何实现无师自通的?
- nssl1488-上升子序列【贪心,dp】
- 计算机通信辞典登录,数字化
- MongoDB数据库操作---mongoose操作
- Android系统(2)---init.rc知识
- oracle rac redo log,RAC共享online redo log和archived log的官方说明
- 通过字符串方法创建新字符串如何打出反斜杠、单引号、双引号、响铃、换行符、水平制表符
- 【论文】AAAI 2020论文解读:关注实体以更好地理解文本
- java判断闰年代码_根据输入的年份判断是否是闰年?(在JAVA用if-else)
- Elasticsearch自动补全(拼音查询自动补全)
- 水晶报表Crystal Reports实例
- javafx-更改程序图标
- Navicat Premium基本使用
- 干货 :什么是数据科学?如何把数据变成产品?
- 阿里云服务器硬盘不够用怎么办?
- 在线支付功能-含银联在线和支付宝
- mt4双线macd_金叉死叉?高手教你MT4的MACD用法
- 微信小程序picker组件仅点击其中的内容才会触发事件
- 【防火墙篇】02. 宽带上网设置 ❀ 网康防火墙
热门文章
- boost::synchronized_value相关的测试程序
- boost::test模块带有自定义初始化测试的静态库使用变体
- boost::hana::decltype_用法的测试程序
- boost::gil::ImageConcept用法的测试程序
- ITK:使用ParallelizeImageRegion
- VTK:可视化算法之BandedPolyDataContourFilter
- VTK:Utilities之ArrayWriter
- VTK:相互作用之RubberBandPick
- OpenCV椭圆拟合ellipse fitting的实例(附完整代码)
- OpenCV捕获正弦波模式