📜  什么是 API?它在 Web 开发中有何用处?

📅  最后修改于: 2022-05-13 01:56:50.095000             🧑  作者: Mango

什么是 API?它在 Web 开发中有何用处?

API代表Application Programming Interface(所有交互的主要参与者)
它就像一个信使,将我们的请求发送到系统并通过无缝连接将响应返回给我们。
我们在许多情况下使用 API,例如为 Web 应用程序获取数据,或连接到具有不断变化的天气等数据的远程服务器,或使两个应用程序能够相互交换数据。
API 不仅提供代码的可重用性,而且还使用了抽象的概念(通过隐藏复杂性来显示功能)。
API 概念在现实生活中最常见的用途包括:

  • 餐厅的服务员将您的订单请求交给厨师并带回所要求的菜肴
  • 配电盘只需按一下即可关闭管灯
  • 从 MMT(基于网络)等网站在线预订航班
  • 从 Facebook 帐户注册购物网站(基于网络)

不同的 API 将以不同的方式进行通信:

  • XML-RCP/SOAP:两者都使用 XML
  • JavaScript:专注于 Javascript
  • RESTful API:使用 HTTP 协议(超文本传输协议)(最适合 Web API)

API 在 Web 开发中的主要功能
API 可用于混搭,即来自一个站点的信息可以与另一个站点的信息混合。身份验证是需要注意的重要事项之一,因为所有 API 都不公开。为安全使用进行身份验证时需要 API 密钥。例如,请参考 Gmail API 认证
有些 API 不需要任何访问令牌。

示例: Github API
https://api.github.com

输出:

{
  "current_user_url": "https://api.github.com/user",
  "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}",
  "authorizations_url": "https://api.github.com/authorizations",
  "code_search_url": "https://api.github.com/search/code?q={query}{&page, per_page, sort, order}",
  "commit_search_url": "https://api.github.com/search/commits?q={query}{&page, per_page, sort, order}",
  "emails_url": "https://api.github.com/user/emails",
  "emojis_url": "https://api.github.com/emojis",
  "events_url": "https://api.github.com/events",
  "feeds_url": "https://api.github.com/feeds",
  "followers_url": "https://api.github.com/user/followers",
  "following_url": "https://api.github.com/user/following{/target}",
  "gists_url": "https://api.github.com/gists{/gist_id}",
  "hub_url": "https://api.github.com/hub",
  "issue_search_url": "https://api.github.com/search/issues?q={query}{&page, per_page, sort, order}",
  "issues_url": "https://api.github.com/issues",
  "keys_url": "https://api.github.com/user/keys",
  "notifications_url": "https://api.github.com/notifications",
  "organization_repositories_url": "https://api.github.com/orgs/{org}/repos{?type, page, per_page, sort}",
  "organization_url": "https://api.github.com/orgs/{org}",
  "public_gists_url": "https://api.github.com/gists/public",
  "rate_limit_url": "https://api.github.com/rate_limit",
  "repository_url": "https://api.github.com/repos/{owner}/{repo}",
  "repository_search_url": "https://api.github.com/search/repositories?q={query}{&page, per_page, sort, order}",
  "current_user_repositories_url": "https://api.github.com/user/repos{?type, page, per_page, sort}",
  "starred_url": "https://api.github.com/user/starred{/owner}{/repo}",
  "starred_gists_url": "https://api.github.com/gists/starred",
  "team_url": "https://api.github.com/teams",
  "user_url": "https://api.github.com/users/{user}",
  "user_organizations_url": "https://api.github.com/user/orgs",
  "user_repositories_url": "https://api.github.com/users/{user}/repos{?type, page, per_page, sort}",
  "user_search_url": "https://api.github.com/search/users?q={query}{&page, per_page, sort, order}"
}

JSON 格式的列表

  1. https://api.github.com/feeds
    输出:
    {
      "timeline_url": "https://github.com/timeline",
      "user_url": "https://github.com/{user}",
      "security_advisories_url": "https://github.com/security-advisories",
      "_links": {
        "timeline": {
          "href": "https://github.com/timeline",
          "type": "application/atom+xml"
        },
        "user": {
          "href": "https://github.com/{user}",
          "type": "application/atom+xml"
        },
        "security_advisories": {
          "href": "https://github.com/security-advisories",
          "type": "application/atom+xml"
        }
      }
    }
    
  2. https://api.github.com/rate_limit
    {
      "resources": {
        "core": {
          "limit": 60,
          "remaining": 56,
          "reset": 1567928119
        },
        "search": {
          "limit": 10,
          "remaining": 10,
          "reset": 1567924666
        },
        "graphql": {
          "limit": 0,
          "remaining": 0,
          "reset": 1567928206
        },
        "integration_manifest": {
          "limit": 5000,
          "remaining": 5000,
          "reset": 1567928206
        }
      },
      "rate": {
        "limit": 60,
        "remaining": 56,
        "reset": 1567928119
      }
    }