在充满令人困惑和不熟悉的首字母缩写词(confusing and unfamiliar acronyms)的互联网上,很容易让人感到困惑。您可能听说过但从未受到启发去研究的一个网络首字母缩写词是“API”。
什么是API?API是应用程序编程接口( application programming interface)的首字母缩写词,是一组例程和协议的名称,这些例程和协议使构建软件应用程序的过程变得更加容易。
查看API的一种简单方法是作为路线图,它告诉软件的组件如何交互。如果不使用API,源代码可能看起来很混乱,令人困惑。
但是,API(APIs)很复杂,需要的不仅仅是简短的总结。在本文中,让我们回顾一下API(APIs)的工作原理以及哪些站点和服务使用它们。
什么是 API,它是如何工作的?(What Is An API and How Does It Work?)
既然我们已经确定API是一组将软件指向正确方向的例程,那么这一切究竟是如何工作的呢?
解释API(API)核心功能的最佳方式是提供一个真实世界的示例。诸如 GrubHub 之类的送餐服务现在(Food)非常(GrubHub)受欢迎,所以让我们讨论一下这些移动应用程序背后的代码是如何工作的。
当您在其中一个应用程序上搜索餐厅或食物时,您会收到基于您的关键字和位置的相关结果。选择结果后,您会看到食品选项、交货时间和更多特定于餐厅的选项。
要安排您的交付,必须在交付网站和数据库之间建立连接——网站是前端,数据库是后端。该数据库存储所有这些餐厅的数据、每个位置的可用性、操作时间表、菜单等。
API是此数据库与呈现其数据的网站或应用程序之间的连接。重要的是要有一个API来创建这种连接,而不是使用硬编码的数据,这主要是因为第三方集成的普及。
例如,如果第三方聚合器能够列出和组织所有可用的餐馆和项目,这对网站来说将是有益的,对吗?如果没有API,如果不使用低效的网络抓取技术,这将是不可能的。
API是负责将数据从数据库传递到应用程序的接口,无论是网站、移动应用程序还是其他任何应用程序。API(APIs)已成为通过 Web 访问和通信应用程序数据的标准方式,每个依赖第三方连接的主要网站或服务都通过提供 API 大大受益。
有哪些类型的 API?(What Types Of APIs Are There?)
有多种不同类型的API协议,但最流行的三种是SOAP(简单对象访问协议(Simple Object Access Protocol))、REST(表示状态传输(Representational State Transfer))和RPC(远程过程调用(Remote Procedure Call))。
SOAP API(SOAP APIs)
SOAP于 1990 年代后期首次引入,它使应用程序能够通过使用网络连接以简单的方式共享资源。SOAP依赖于标准协议,例如HTTP和SMTP,由于此类协议的普及,这使得它能够在几乎所有环境中使用。
它的主要优势在于它被广泛使用和建立。如果它没有坏,就不要修理它。
REST API(REST APIs)
REST 由Roy Fielding于 2000 年引入。它的直接目标是作为对广泛采用SOAP所产生的许多问题的回应。
与SOAP类似,REST依赖HTTP在应用程序之间传输信息。但是,它与SOAP(SOAP)的一个主要区别在于它支持JSON,后者需要通过XML数据格式传输数据。JSON是一种数据格式,许多人认为它更易于读写。此外,REST API(REST APIs)可以缓存数据,从而获得更好的性能。
根据Cloud Elements 2017 年的一份报告, REST 现在占所有API(APIs)的 80% 以上。
RPC API(RPC APIs)
RPC API(RPC APIs)有多种形式,但正如我们从REST API(REST APIs)中了解到的,JSON是一种非常流行的数据格式,因此JSON-RPC是它最流行的。
JSON-RPC是最适合极简主义者和依赖其API直接性的用户的协议。与SOAP(SOAP)和REST相比,它的范围要窄得多,而且它的灵活性和命令集非常有限——但对于一些开发人员来说,少即是多。
还有其他基于 RPC 的API类型,例如 gRPC,但如果您的优先级是实现的简单性,那么JSON-RPC是首选。(JSON-RPC)
谁使用 API?(Who Uses APIs?)
任何类型的开发人员,无论是前端还是后端,都应该有使用API(APIs)的经验。在网站或 Web 服务方面,几乎所有主要参与者都有某种可用的API — 最明显的是Windows。
由于Microsoft Windows是这个星球上最流行的操作系统,因此应用程序开发人员自然需要一套关于如何与其 UI 交互的指南。如果无法访问Windows API,编写一个严重依赖于与操作系统交互的应用程序将是一个令人头疼的问题。
那里有成千上万的API(APIs),有些是免费的,有些是付费的。这里只是一些值得研究的例子:
API(APIs)是网络上一些最大和最重要的网站和应用程序背后的关键连接器之一。虽然普通用户可能无法看到API(APIs)提供的好处,但它们对于开发人员和 Web 服务来说是天赐之物。
HDG Explains: What Is An API?
On an internet filled with confusing and unfamiliar acronyms, it can be easy to become confused. One web acronym you may have heard but never felt inspired to look into is “API”.
What is an API? API is an acronym for application programming interface, and is a name for a set of routines and protocols that make the process of building software applications much easier.
A simple way to look at an API is as a roadmap that tells the components of a piece of software how to interact. Without the use of an API, source code could look like a jumbled, confusing mess.
However, APIs are complex and require more than a brief summarization. In this article, let’s go over how APIs work and what sites and services use them.
What Is An API and How Does It Work?
Now that we have established that an API is a set of routines that point software in the right direction, how exactly does this all work?
The best way to explain the core functionality of an API is to provide a real-world example. Food delivery services, such as GrubHub, are incredibly popular right now, so let’s discuss how the code behind mobile apps like these might work.
When you search for a restaurant or item of food on one of these apps, you receive relevant results based on your keywords and location. Upon selecting a result, you’re presented with food item options, delivery times, and more restaurant-specific options.
To schedule your delivery, a connection between the delivery website and a database must occur—the website being the frontend and the database being the backend. The database is what stores the data for all of these restaurants, their availability per location, operation schedule, menus, and more.
The API is the connection between this database and the website or app that is presenting its data. It’s important that an API is present to create this connection, rather than using hard-coded data, primarily because of the popularity of third-party integrations.
For example, it would be beneficial for a website if third-party aggregators were able to list and organize all of the restaurants and items it has available, right? Without an API, this wouldn’t be possible without using inefficient web-scraping techniques.
The API is the interface responsible for delivering the data from the database to the application, be it a website, mobile app, or anything else. APIs have become the standard way of accessing and communicating application data across the web, and every major website or service that depends on third-party connectivity greatly benefits by providing one.
What Types Of APIs Are There?
There are multiple different types of API protocols, but the three most popular are SOAP (Simple Object Access Protocol), REST (Representational State Transfer), and RPC (Remote Procedure Call).
SOAP APIs
SOAP was first introduced in the late 1990s and enabled applications to share resources in a simple way through using network connections. SOAP relies on standard protocols, such as HTTP and SMTP, which enable it to be used across practically every environment due to the popularity of such protocols.
Its main strength is that it is widely used and established. If it ain’t broke, don’t fix it.
REST APIs
REST was introduced in 2000 by Roy Fielding. Its immediate goal was to act as a response to many of the issues that were created by the widespread adoption of SOAP.
Similar to SOAP, REST relies on HTTP to transfer information between applications. However, one major difference that sets it apart from SOAP, which requires data to be transmitted via the XML data format, is that it supports JSON. JSON is a data format that many agree is easier to read and write. Additionally, REST APIs can cache data, allowing for much better performance.
REST now accounts for more than 80% of all APIs, according to a 2017 report by Cloud Elements.
RPC APIs
RPC APIs come in many flavors, but as we learned from REST APIs, JSON is a very popular data format, so JSON-RPC is its most popular.
JSON-RPC is a protocol best suited for minimalists and users who rely on the straightforwardness of their API. Its much narrower in scope when compared to SOAP and REST, and it’s very limited in its flexibility and set of commands—but for some developers, less is more.
There are other RPC-based API types, such as gRPC, but JSON-RPC is the go-to if your priority is the simplicity of implementation.
Who Uses APIs?
Developers of any kind, be it frontend or backend, should have experience working with APIs. In terms of websites or web services, practically all of the major players have some sort of API available—the most obvious being Windows.
With Microsoft Windows being the most popular operating system on the planet, it’s only natural that application developers will need a set of guidelines for how to interact with its UI. Without access to the Windows API, programming an application that relies heavily on interacting with the operating system would be a major headache.
There are thousands of APIs out there, some free and some paid. Here are just a few examples worth looking into:
- Google Play Developer API: Publishing and app-management tasks related to Google Play
- Skyscanner APIs: Travel-based APIs relating to flights, car hires, and more
- Twilio API: Allows programmatic sending and receiving of phone calls
- Discord API: Allows users to create the bots that make Discord the messaging service it is today
- IPinfo API: On-the-fly IP address and geolocation data for your website or app
APIs are one of the key connectors behind some of the web’s largest and most important websites and applications. While the average user may not be able to see the benefit that APIs provide, they are a godsend to developers and web services.