了解Web API

While they can be intimidating at first, APIs are pretty straightforward once you understand their core purpose… and when you do, an entire range of functionality in web development becomes available to you.

尽管一开始它们会令人生畏,但一旦您了解了API的核心目的,它们就非常简单了……当您这样做时,Web开发中的全部功能就可以使用了。

An API - standing for Application Program Interface - is a method for getting some result from a request. An old-school calculator might be thought of as a mathematical API: providing the device with some numbers and an operator (divide, multiply, etc) produces a result. At the physical level, a Rock 'Em Sock 'Em Robot set might be considered an API: twiddle with some levers, and watch the robots fight.

API代表应用程序接口-是一种从请求中获取某些结果的方法。 老式计算器可能被认为是一种数学API:为设备提供一些数字,然后使用一个运算符(除法,乘法等)产生结果。 在物理层面上,Rock'Em Sock'Em机器人集可能被认为是一种API:用一些操纵杆旋转,观察机器人在战斗。

APIs were one of the foundational components of modern personal computing. Without them, programmers would have to build every program completely from scratch. Using an API, they can build applications much faster: rather than coding a Close Window button, a programmer can request it from the operating system’s API, importing the functionality into their application, without having to worry about the operation or performance of the UI. APIs also work at the hardware level, allowing different devices and components to communicate with each other.

API是现代个人计算的基本组成部分之一。 没有它们,程序员将不得不完全从头开始构建每个程序。 他们可以使用API​​来更快地构建应用程序:程序员无需编写“ Close Window按钮,而是可以从操作系统的API请求它,将功能导入到他们的应用程序中,而不必担心UI的操作或性能。 API还可以在硬件级别运行,从而允许不同的设备和组件相互通信。

Web API (Web APIs)

On the web, APIs take two forms: client-side and server-side.

在网络上,API采取两种形式: 客户端和服务器端

  1. Client-side APIs The HTML5 specification provides a series of APIs to developers for use on web sites, such as the Fullscreen and Dialog APIs. These are accessed via , and extend the functionality of the browser to replace native applications. This functionality has caused many in the industry to refer to sites that use these features extensively as “web applications” or “web apps”.

    客户端API HTML5规范为开发人员提供了一系列API,可在网站上使用,例如Fullscreen和Dialog API 。 这些可以通过进行访问,并扩展了浏览器的功能以替换本机应用程序。 此功能已导致业界许多人将广泛使用这些功能的网站称为“ Web应用程序”或“ Web应用程序”。

  2. Server-side APIs Many companies produce their own APIs: Netflix, for example, has an API that shows the films currently in circulation for subscribers to browse. There are thousands of APIs in existence: some of them for public use, some reserved for use solely inside corporate web sites. These data stores are accessed by making a request from a server.

    服务器端API许多公司都有自己的API:例如Netflix,有一个API,它可以显示当前正在发行的影片供订户浏览。 有成千上万的API :其中一些API供公众使用,有些保留供公司网站内部使用。 通过从服务器发出请求来访问这些数据存储。

This article concentrates on the second sense of the term.

本文重点介绍该术语的第二种含义。

使用Web API (Using Web APIs)

Google Maps has a fully-fledged API, but we can see a simple variation of it when we bring up a map in a browser. For example, the following URL:

Google Maps具有完善的API,但是在浏览器中显示地图时,我们可以看到它的简单变化。 例如,以下URL:

https://www.google.ca/maps/@51.0890235,-114.0293939,12z

https://www.google.ca/maps/@51.0890235,-114.0293939,12z

…brings up my current approximate location in the city of Calgary. By changing the first two pairs of numbers after the @ (representing latitude and longitude), we change the information requested from the Google Maps API, and gain a different result.

…得出我目前在卡尔加里市的大概位置。 通过更改@之后的前两对数字(代表纬度和经度),我们更改了Google Maps API请求的信息 ,并获得了不同的结果。

In real-world use, web sites make calls to APIs using , , Ruby or another language. Most web APIs return information in JSON, XML or HTML; many have an option for all three. Some APIs require a unique key (allowing the service to track requests and use), others do not. Some APIs have excellent documentation; some do not.

在实际使用中,网站使用 ,Ruby或其他语言来调用API。 大多数Web API都以JSONXMLHTML返回信息; 许多都有这三个选项。 一些API需要唯一密钥 (允许服务跟踪请求和使用),而其他API则不需要。 一些API具有出色的文档; 有些没有。

As one example, the OpenWeatherMap API can take a URL that contains a city name and an API key:

举一个例子, OpenWeatherMap API可以采用包含城市名称和API密钥的URL:

http://api.openweathermap.org/data/2.5/weather?q=Calgary
&appid=91f05e4330f6e85cab273b8b1ad8bb71

Used directly in the URL bar of a browser the returns data that looks like the following:

直接在浏览器的URL栏中使用的返回数据如下所示:

{"coord":{"lon":-114.09,"lat":51.05},"weather":[{"id":
800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"cmc 
stations","main":{"temp":281.645,"pressure":868.29,"humidity":70,"temp_min":
281.645,"temp_max":281.645,"sea_level":1028.59,"grnd_level":868.29},"wind":{"speed":
1.07,"deg":175.501},"clouds":{"all":0},"dt":1462251211,"sys":{"message":
0.0031,"country":"CA","sunrise":1462277067,"sunset":1462330973},"id":
5913490,"name":"Calgary","cod":200}

As you can see, the data is in JSON format, and contains information about the current weather conditions here in Calgary. Naturally, the typical user would never see the data in this way: I’d make the request via JavaScript or another language, possibly in combination with a wrapper (a helper script to organise and format the returned data) and present the result on the page surrounded by nice semantic HTML. By using appropriate APIs, it's possible to build a far more fully-featured site than one you could reasonably create by yourself.

如您所见,数据为JSON格式,其中包含有关卡尔加里当前天气状况的信息。 自然地,典型的用户永远不会以这种方式看到数据:我会通过JavaScript或另一种语言(可能与包装器(用于对返回的数据进行组织和格式化的帮助程序脚本)结合使用)发出请求,并在页面被漂亮的语义HTML包围。 通过使用适当的API,可以构建比您自己合理创建的站点功能更强大的站点。

API calls can be far more specific in the data they return, depending on the query: I could request only the current wind speed for my location, for example, or historical temperature records for today’s date.

API调用可以在它们返回的数据,根据查询更具体:我能为我的位置请求当前风速,例如,或历史温度记录今天的日期。

I’ll be providing examples of just that and many other uses of APIs in future articles; follow me on Twitter to receive updates the moment they are posted.

我将在以后的文章中提供有关API的示例以及其他许多用法。 在Twitter上关注我,以在发布最新消息后立即收到它们。

翻译自: https://thenewcode.com/1078/Understanding-Web-APIs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值