[英]How to create API
I'm finding a lot of tutorials on how to implement APIs but how does one actually create one?我找到了很多关于如何实现 API 的教程,但实际上如何创建一个? For example, I want to query movie quotes from my DB and also insert new ones via an API.例如,我想从我的数据库中查询电影报价并通过 API 插入新的报价。 I know PHP and MySQL but what am I missing to make an API?我知道 PHP 和 MySQL,但是我缺少什么来制作 API? I read about the REST method and it seems easy but I can't find a step-by-step guide.我阅读了 REST 方法,看起来很简单,但找不到分步指南。
Can someone please share an example or some steps to creating a simple API?有人可以分享一个示例或一些创建简单 API 的步骤吗?
You may be interested in:您可能对以下内容感兴趣:
Worth doing Sarfraz's article to understand design and then the mechanics/plumbing.值得做 Sarfraz 的文章来了解设计,然后是机械/管道。 Once you understand what your API looks like, definitely go back an evaluate REST frameworks available for PHP or whatever language you're working with.一旦您了解了您的 API 是什么样子,一定要回去评估可用于 PHP或您正在使用的任何语言的REST 框架。 Friends don't let friends right their own rest plumbing in 2010. 2010 年,朋友们不要让朋友们纠正自己的休息管道。
Beyond just making it send and receive xml/json/etc., the idea of what makes a good API is something many have considered-- worth reading up on imo.除了让它发送和接收 xml/json/ 等,很多人都考虑过什么是好的 API 的想法—— 值得在imo 上阅读。 People who are smarter than me have said that great APIs that are designed with the way developers user them in mind, first and foremost... from there they work backwards into a black box their consumers shouldn't have to care about.比我更聪明的人说过,伟大的 API 设计时考虑到了开发人员使用它们的方式,首先……从那里他们向后工作到他们的消费者不必关心的黑匣子中。
If you are not restricted to implementing the API in PHP, here is a quick setup guide - for NodeJS based REST APIs.如果您不限于在 PHP 中实现 API,这里有一个快速设置指南- 适用于基于 NodeJS 的 REST API。 This talks about implementing a simple ping API in NodeJS, building and releasing it to production.这讨论了在 NodeJS 中实现一个简单的 ping API,构建并发布到生产环境。
Prerequisites先决条件
Setup your codebase设置您的代码库
Set some local environment variables to use later设置一些本地环境变量以备后用
export AWS_REST_SERVICE_NAME=MyRestService
export AWS_REST_SERVICE_S3_BUCKET_NAME=MyRestService # This has to be unique globally
Clone my sample repository.克隆我的示例存储库。 This has a simple ping GET Api written in NodeJS 8.1这有一个用 NodeJS 8.1 编写的简单 ping GET Api
git clone https://github.com/sarthakj178/AWSRestServiceTemplate.git $AWS_REST_SERVICE_NAME
cd $AWS_REST_SERVICE_NAME
npm install
That's it.就是这样。 Your REST Service is setup locally.您的 REST 服务是在本地设置的。 Test it out.测试一下。
sam local start-api # This will start your service locally on port 3000
curl -X GET "http://localhost:3000/ping" # On a different tab. Should return {"success":true,"message":"Hello world"}
Release to production发布到生产
aws s3 mb s3://$AWS_REST_SERVICE_S3_BUCKET_NAME
sam package --template-file template.yaml --s3-bucket $AWS_REST_SERVICE_S3_BUCKET_NAME --output-template-file packaged.yaml
sam deploy --template-file ./packaged.yaml --stack-name $AWS_REST_SERVICE_NAME --capabilities CAPABILITY_IAM
Verify your service in production在生产中验证您的服务
Go to AWS Console -> API Gateway -> YOUR_REST_SERVICE_NAME -> Stages -> Prod -> ping -> GET转到 AWS 控制台 -> API 网关 -> YOUR_REST_SERVICE_NAME -> Stages -> Prod -> ping -> GET
You will see a URL similar to https://xxxx.execute-api.rr-rrrrr-r.amazonaws.com/Prod/ping .您将看到类似于https://xxxx.execute-api.rr-rrrrr-r.amazonaws.com/Prod/ping的 URL。
This is your REST Service in production.这是您在生产中的 REST 服务。 Hit it, you'll see a response same as what you saw on your local desktop.点击它,您将看到与您在本地桌面上看到的相同的响应。
您可以在此处创建示例 api sample api
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.