005精讀 | 4萬高贊神文:什麼是API?

2021-01-14 極客英語精讀計劃

Before I learned software development, API sounded like a kind of beer.

在我學習軟體開發之前,API聽起來就像啤酒。

Today I use the term so often that I have in fact recently tried to order an API at a bar.

在今天我頻繁地使用這個術語,以至於我最近在酒吧點了一杯API。

The bartender’s response was to throw a 404: resource not found.

調酒師的反應是拋出404:資源未找到。(大概就是黑人問號臉)

I meet lots of people, both working in tech and elsewhere, who have a rather vague or incorrect idea about what this fairly common term means.

我遇到很多人,包括在科技行業和其他行業工作的人,他們對這個相當常見的術語的含義有相當模糊或錯誤的想法。

Technically, API stands for Application Programming Interface. At some point or another, most large companies have built APIs for their customers, or for internal use.

從技術上講,API代表應用程式編程接口。在某種程度上,大多數大公司都為客戶或內部使用構建了api。

But how do you explain API in plain English? And is there a broader meaning than the one used in development and business? First, let’s pull back and look at how the web itself works.

但是如何用簡單的英語解釋API呢?還有比開發和業務中使用的含義更廣泛的含義嗎?首先,讓我們回顧一下web本身是如何工作的。

WWW and remote servers

When I think about the Web, I imagine a large network of connected servers.

當我想到Web時,我想像一個由連接的伺服器組成的大型網絡。

Every page on the internet is stored somewhere on a remote server. A remote server is not so mystical after all — it’s just a part of a remotely located computer that is optimized to process requests.

網際網路上的每個頁面都存儲在遠程伺服器的某個位置。遠程伺服器一點也不神秘——它只是遠程計算機的一部分,用於優化處理請求。

To put things in perspective, you can spin up a server on your laptop capable of serving an entire website to the Web (in fact, a local server is what engineers use to develop websites before releasing them to the public).

客觀地說,你可以在你的筆記本電腦上啟動一個伺服器,該伺服器能夠將整個網站服務於Web(實際上,工程師在向公眾發布網站之前使用本地伺服器來開發網站)。

When you type www.facebook.com into your browser, a request goes out to Facebook’s remote server. Once your browser receives the response, it interprets the code and displays the page.

當你在瀏覽器中輸入www.facebook.com時,一個請求就會發送到Facebook的遠程伺服器。一旦瀏覽器接收到響應,它將解釋代碼並顯示頁面。

To the browser, also known as the client, Facebook’s server is an API. This means that every time you visit a page on the Web, you interact with some remote server’s API.

對於瀏覽器,也稱為客戶端,Facebook的伺服器是一個API。這意味著每次訪問Web頁面時,您都要與某個遠程伺服器的API進行交互。

An API isn’t the same as the remote server — rather it is the part of the server that receives requests and sends responses.

API與遠程伺服器不同——它是伺服器接收請求和發送響應的部分。

APIs as a way to serve your customers

You』ve probably heard of companies packaging APIs as products. For example, Weather Underground sells access to its weather data API.

API是服務客戶的一種方式

你可能聽說過將api打包為產品的公司。例如,Weather Underground出售對其天氣數據API的訪問權。

Example scenario: Your small business’s website has a form used to sign clients up for appointments. You want to give your clients the ability to automatically create a Google calendar event with the details for that appointment.

示例場景:你的企業網站有一個用於為客戶註冊預約的表單。你希望讓你的客戶端能夠自動創建帶有該約會詳細信息的谷歌日曆事件。

API use: The idea is to have your website’s server talk directly to Google’s server with a request to create an event with the given details. Your server would then receive Google’s response, process it, and send back relevant information to the browser, such as a confirmation message to the user.

API的使用:這個想法是讓你的網站的伺服器直接與谷歌的伺服器進行對話,請求用給定的細節創建一個事件。然後,你的伺服器將接收谷歌的響應,處理它,並將相關信息發送回瀏覽器,例如發送給用戶的確認消息。

Alternatively, your browser can often send an API request directly to Google’s server bypassing your server.

或者,您的瀏覽器通常可以繞過您的伺服器直接向谷歌的伺服器發送API請求。


How is this Google Calendar’s API different from the API of any other remote server out there?


這個谷歌日曆的API與其他遠程伺服器的API有什麼不同?


In technical terms, the difference is the format of the request and the response.


在技術術語中,區別在於請求和響應的格式。


To render the whole web page, your browser expects a response in HTML, which contains presentational code, while Google Calendar’s API call would just return the data — likely in a format like JSON.


要呈現整個web頁面,瀏覽器需要HTML格式的響應,其中包含表示代碼,而谷歌Calendar的API調用只返回數據—可能是JSON格式。


If your website’s server is making the API request, then your website’s server is the client (similar to your browser being the client when you use it to navigate to a website).


如果你的網站伺服器發出API請求,那麼你的網站伺服器就是客戶端(類似於您的瀏覽器在使用它導航到網站時是客戶端)。


From your users perspective, APIs allow them to complete the action without leaving your website.


從用戶的角度來看,API允許他們在不離開網站的情況下完成操作。


Most modern websites consume at least some third-party APIs.


大多數現代網站至少使用一些第三方api。


Many problems already have a third-party solution, be it in the form of a library or service. It’s often just easier and more reliable to use an existing solution.


許多問題已經有了第三方解決方案,可以是庫或服務的形式。使用現有的解決方案通常更容易、更可靠。


It’s not uncommon for development teams to break up their application into multiple servers that talk to each other via APIs. The servers that perform helper functions for the main application server are commonly referred to as microservices.


對於開發團隊來說,將他們的應用程式分解成多個通過api相互通信的伺服器並不少見。為主應用伺服器執行輔助功能的伺服器通常稱為微服務


To summarize, when a company offers an API to their customers, it just means that they』ve built a set of dedicated URLs that return pure data responses — meaning the responses won’t contain the kind of presentational overhead that you would expect in a graphical user interface like a website.


總而言之,當一家公司向其客戶提供API時,這僅僅意味著他們構建了一組返回純數據響應的專用url——這意味著響應不會包含你在像網站這樣的圖形用戶界面中所期望的表示開銷。


Can you make these requests with your browser? Often, yes. Since the actual HTTP transmission happens in text, your browser will always do the best it can to display the response.


你可以在瀏覽器中發出這些請求嗎?通常,是的。由於實際的HTTP傳輸是在文本中進行的,因此瀏覽器將始終盡力顯示響應。


For example, you can access GitHub’s API directly with your browser without even needing an access token. Here’s the JSON response you get when you visit a GitHub user’s API route in your browser  (https://api.github.com/users/petrgazarov):


例如,你可以使用瀏覽器直接訪問GitHub的API,甚至不需要訪問令牌。下面是當你在瀏覽器中訪問GitHub用戶的API路由時所得到的JSON響應 (https://api.github.com/users/petrgazarov):



The browser seems to have done just fine displaying a JSON response. A JSON response like this is ready for use in your code. It『s easy to extract data from this text. Then you can do whatever you want with the data.


瀏覽器似乎已經很好地顯示了JSON響應。這樣的JSON響應可以在代碼中使用。從文本中提取數據很容易。然後你可以對數據做任何你想做的事情。

A is for 「Application」

To close off, let’s throw in a couple more examples of APIs.

最後,讓我們再看幾個api示例。

「Application」 can refer to many things. Here are some of them in the context of API:

「應用」可以指很多東西。以下是API環境下的一些例子:

1、A piece of software with a distinct function.

1、一種功能獨特的軟體。

2、The whole server, the whole app, or just a small part of an app.

2、整個伺服器,整個應用,或應用的一小部分。

Basically any piece of software that can be distinctively separated from its environment, can be an 「A」 in API, and will probably also have some sort of API.

基本上,任何軟體都可以與它的環境區別開來,可以是API中的「A」,並且可能也會有某種API。

Let’s say you’re using a third-party library in your code. Once incorporated into your code, a library becomes part of your overall app. Being a distinct piece of software, the library would likely have an API which allows it to interact with the rest of your code.

假設你在代碼中使用第三方庫。一旦你的代碼中包含了一個庫,它就會成為你整個應用程式的一部分。作為一個獨立的軟體,這個庫可能會有一個API,它可以與你的其他代碼進行交互。

Here’s another example: In Object Oriented Design, code is organized into objects. Your application may have hundreds of objects defined that can interact with one another.

下面是另一個例子:在面向對象的設計中,代碼被組織成對象。你的應用程式可能定義了數百個可以相互交互的對象

Each object has an API — a set of public methods and properties that it uses to interact with other objects in your application.

每個對象都有一個API——一組用於與應用程式中的其他對象交互的公共方法和屬性。

An object may also have inner logic that is private, meaning that it’s hidden from the outside scope (and not an API).

對象也可能具有私有的內部邏輯,這意味著它隱藏在外部範圍之外(而不是API)

From what we have covered, I hope you take away the broader meaning of API as well as the more common uses of the term today.

從我們已經討論過的內容中,我希望您能夠理解API的更廣泛含義以及這個術語現在更常用的用法。

Interesting Resources (stuff that I left out but is still very cool):

有趣的資源(我省略了但仍然很酷的東西):

about DNS:https://www.youtube.com/watch?v=72snZctFFtA&feature=youtu.be

about HTTP:https://simple.wikipedia.org/wiki/Hypertext_Transfer_Protocol

相關焦點

  • 005 什麼是加速度
    005 What is Acceleration005 什麼是加速度
  • api 微博數據專題及常見問題 - CSDN
    如果只是為了收集數據可以諮詢我的郵箱,如果是為了學習爬蟲,建議改學phantomjs從網頁中爬取微博的)利用新浪API實現數據的抓取(由於api接口限制增大,本文已基本廢棄) 2018.5.16 提示 微博的api接口現在已經不好用了,普通權限的token已經爬不到什麼數據了,想要用這個代碼爬大量數據的已經不太可能,只能作為熟悉微博api
  • 手把手教你學Numpy——常用API合集
    實際上numpy的內部會將高維數組轉化成一維之後再進行這個操作,我們可以reshape一下數組來進行驗證:這些只是api的基本用法,numpy當中支持的功能不僅如此。我們觀察一下這些函數會發現,它們的作用域都是一組數據,返回的是一組數據通過某種運算得到的結果。舉個例子,比如sum,是對一組數據的價格。std計算的是一組數據的標準差,這樣的函數我們稱為聚合函數。
  • obgame分享:電競LOL數據API接口-API調用
    接口請求地址:http://api.xxe.io/?LPL", "match_desc": "LPL是中國地區英雄聯盟職業聯賽,是LOL在大陸地區的最高級別賽事,擁有超高人氣", "match_num": "5", "match_sorting": "1", "match_type": "2", "match_bonus": "500萬人民幣
  • Restful Api-接口設計
    (4) URL中通常不出現動詞,只有名詞 (5) 使用JSON不使用XML設計方式 HEAD(SELECT)只獲取某個資源的頭部信息 GET(SELECT)獲取資源 POST(CREATE)創建資源 PATCH(UPDATE)更新資源的部分屬性(很少用,一般用POST
  • 精讀|《昆蟲記-10菜豆象》精讀批註
    李曉 南京六合區初中語文教師 新課標讀書會共讀成員 圖片:網絡/新課標讀書會 文字:網絡/李曉 排版:林小新 本期的「精讀批註
  • 分享幾個在線生成網址二維碼的API接口
    都是採用http協議接口,無需下載安裝什麼軟體,可簡單方便地引用,這才是最簡單、最便捷的免費網址二維碼生成工具。        1.http://pan.baidu.com/share/qrcode?
  • dedecms添加百度地圖api可動態放縮,支持http和https的方法
    1.4版本之後就需要註冊開發者獲取自己獨立的密鑰才可以。<scripttype="text/javascript"src="//api.map.baidu.com/api?--div一定要設置寬和高--><script type="text/javascript">// 百度地圖API功能var map = new BMap.Map("allmap");
  • 社會學吧群學精讀會擴招啦!
    經過前3期的運作,為了更好集中閱讀興趣,現對群學精讀會進行改版、擴招。歡迎大家參與。一、群學精讀會新規則1、新版「群學精讀會」分成八個小組,每個小組8-16人。「群學精讀會」每期1個月,從2017年5月至12月,每月閱讀一本書。書目由社會學吧提供目錄,然後由成員共同決定需要閱讀的8本書。
  • 使用C#的後端Web API:循序漸進教程
    除了這些語言的語法特徵外,還有許多其他問題/問題,例如擴展的可能性,不同類型資料庫的使用,高學習曲線,容錯要求,大量數據等等。上。哪種語言最受歡迎?你應該使用哪一個?也許有人會推薦PHP,它具有豐富的功能和較低的學習曲線。然而,事實仍然是現在最常用的語言是Java和.NET。
  • 逆水寒api-ms-win-crt-runtime-11-1-0.dll丟失怎麼解決?
    逆水寒少部分玩家更新遊戲後出現提示計算機丟失api-ms-win-crt-runtime-11-1-0.dll,無法啟動逆水寒。逆水寒api-ms-win-crt-runtime-11-1-0.dll丟失怎麼解決?99單機網小編分享下逆水寒api-ms-win-crt-runtime-11-1-0.dll丟失解決方法。
  • Baidu與Google地圖API初探
    前天周六,有個好友過來玩,他說想在他的網站中加入地圖導航模塊,但不知道選擇哪個第三方Map API 在網上查了下Baidu、Google、QQ和MapBar等4種Map API(都是採用JS開放API),也查看了它們的SDK開發文檔,
  • 百度快速收錄API提交代碼適合各種網站及APP教程
    2、快速收錄工具可以向百度搜索主動推送資源,縮短爬蟲發現網站連結的時間,對於高實效性內容推薦使用快速收錄工具,實時向搜索推送資源。3、資源不符合移動資源標準,依然會佔用配額,請您謹慎選擇提交類型。判斷站點是否符合移動體驗標準,請參考《百度搜索優質內容指南》及《百度APP移動搜索落地頁體驗白皮書5.0》。4、如果網站已通過HTTPS認證工具驗證為HTTPS網站,請確保所提交連結的協議頭為HTTPS。
  • 鯨魚小班原版精讀課,讓女兒的英語原版精讀之旅不再孤單!
    初始在教材選擇上我絲毫沒有猶豫,著名出版社的讀物一定是首選,於是牛津閱讀樹成為了我給女兒選擇的第一本英語讀物,最開始主要是我讀她聽,一年後,大約到她4歲時,她已經可以獨立閱讀牛津樹Read with Biff,Chip&Kipper系列了,我特別驚喜。
  • 新浪微博 API開發簡介之 Python篇
    於是照著寫了一個:# -*- coding: utf-8 -*-from weibopy.auth import OAuthHandlerfrom weibopy.api import APIconsumer_key= '應用的key'consumer_secret ='應用的App Secret'
  • 基於Django和翻譯API實現web版的中英文對照翻譯(一)
    一番了解之後,決定選用谷歌翻譯/搜狗翻譯/有道翻譯官方提供的翻譯api自己實現一個web版的翻譯界面。目前搜狗翻譯/有道翻譯都已經開始收費,但收費的標準還可以讓人接受,以搜狗翻譯為例,現價:40.00/百萬字符。還是可以接受的。閒言少敘。直入正題。
  • API雷射幹涉儀在測量直線模組中的應用
    直線電機也稱線性電機,線性馬達,直線馬達,推桿馬達,是一種新型電機,具有高加速、高減速、運動慣量小、高靈敏、系統剛性好、使用壽命長、安全可靠等優點
  • SCP系列 I:萬能鑰匙 SCP-005 ——不老泉 SCP-006
    那接下來我們講講SCP-005之前提到過的萬能鑰匙。萬能鑰匙 項目編號SCP-005 項目等級 Safe 屬於能被安全收容的異常那麼萬能鑰匙又有什麼能力呢?從直觀上看,SCP-005是一把裝飾華麗的20世紀的老式鑰匙。據說當時發現它的時候一個平民正在用它潛入一棟守衛森嚴的設施。SCP-005看上去很容易打開任何形式的鎖。
  • Rocket-API 2.3.2 發布,基於 spring boot 的 API 敏捷開發框架
    功能新增或修改: 遠程部署重複判斷 添加mongo對象操作函數 mapping路徑匹配修改,解決restful下路徑可能會匹配執行邏輯錯誤問題 增加配置項 駝峰轉換配置 spring.rocket-api.map-underscore-to-camel-case
  • SCP-005,萬能鑰匙,真正意義上的萬能鑰匙
    從直覺角度來說,SCP-005看似並沒有什麼危險性,即便如此,但它獨特的功能也會讓它變得十分危險。在SCP-005被發現時,一名平民正在用它潛入一棟守衛森嚴的設施,它擁有著能輕易打開任何鎖的能力,不論那鎖是機械的還是數字的,目前該能力的來源尚無法判斷。