行雲者研發基地
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: 什麼是 API? tags: 研究, 網頁組, 前端 --- # 什麼是 API? [TOC] 什麼是 API?讓我們先看一下影片: <iframe width="640" height="360" src="https://www.youtube.com/embed/zvKadd9Cflc" title="什麼是 API?" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ## 所以什麼是一個 API? API 全名是 Application Programming Interface,字面解釋就是應用程式串接的介面。 所以其實只要是給程式串接用的,就可以算是 API,並不限於在網站上才叫做 API。 但我們今天先以網頁的部分來說。 ## API 其實跟一般網頁差不多 沒錯,API 其實跟一般的網頁差不多。 舉例來說,你看到的網頁可能長這樣: | 程式 | 顯示 | | ------------------------------------ | ------------------------------------ | | ![](https://i.imgur.com/eqCWccZ.png) | ![](https://i.imgur.com/jFwh5CQ.png) | 實際上,這些內容也是資料經過美化(或視覺化)過後呈現給你的,就叫做網頁。 而沒有美化過的,只有資料的,就叫做 API,像是這樣: | 程式 | 顯示 | | ------------------------------------ | ------------------------------------ | | ![](https://i.imgur.com/CS5S6z9.png) | ![](https://i.imgur.com/sj0eFSu.png) | 所以,在網頁中,API 與一般網頁的差異在哪裡? **最主要的**差異就只是回傳的東西不同,一個是美美的畫面,一個是純粹的資料。 | 網頁:畫面(HTML) | API:資料(JSON) | | ------------------------------------ | ------------------------------------ | | ![](https://i.imgur.com/opaROwV.png) | ![](https://i.imgur.com/mYGT8Tr.png) | ## 只有內容的不同嗎? 當然,除了內容不同之外,用法也有一些差異。 ## HTTP 請求 在網頁的請求 (request) 中,[有很多不同的請求方法 (method)](https://www.w3schools.com/tags/ref_httpmethods.asp)。 我們先講兩種最重要的,有 GET 跟 POST 兩種請求方法。 ### Get 請求 最基礎的就是 Get 請求,其實你現在也正在做這件事情。 只要你打開瀏覽器,瀏覽任何一個網頁的時候都是使用 Get 請求在跟網站互動。 像是你打開 Google,就是發送一個 Get 請求給 www.google.com。 #### 攜帶參數 如果我們需要攜帶一些參數給網頁,就可以透過在網址上面加入特定規則的內容提供給伺服器。 像是如果我們在 Google 進行搜尋「Http 請求 method」,就會是下面這個網址: https://www.google.com/search?q=http+%E8%AB%8B%E6%B1%82+method 其中 `https://www.google.com/search` 是我們送請求的目標網址,而從 ? 開始的 `?q=http+%E8%AB%8B%E6%B1%82+method` 則是我們這次附帶的參數。 其中這個裡面攜帶了一個參數而已,名字叫做 `q`,則參數內容是 `http+%E8%AB%8B%E6%B1%82+method`。由此可以看的出來,一個變數是使用 = 分割,前面是變數名字,後面是內容。不過內容怎麼長得很特別呢?內容其實是「Http 請求 method」經過 URI [百分號編碼](https://zh.wikipedia.org/wiki/%E7%99%BE%E5%88%86%E5%8F%B7%E7%BC%96%E7%A0%81) 過後的結果。 若想要在網頁中手動達成,可以透過原生的 [encodeURI](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) 等等方法操作。 #### 攜帶多個參數 可是上面只有一個參數,如果想要多個呢?就用 & 來分隔即可。 舉例來說,如果你到 YouTube Music 任何一個影片點分享,可能會拿到下面這樣的連結: https://music.youtube.com/watch?v=wLNTAMY1NZA&feature=share 就可以看到這個網址裡面包含兩個變數: | 名稱 | 內容 | | ------- | ----------- | | v | wLNTAMY1NZA | | feature | share | ### Post 請求 Post 請求就有點不一樣了,單純地透過瀏覽器打網址,是無法送出 Post 請求的。 要在網頁中送出 Post 請求,要透過 HTML 中的表單 (Form) 或是透過 JavaScript 進行請求。 而攜帶參數的方法,除了可以跟 Get 一樣透過網址攜帶之外,還可以透過 HTTP Body 帶入更多內容。 HTTP Body 比起單純使用網址,有可以傳送更長的資料、更多種的資料(像是圖片、影片)等等優勢,且因為不是在網址,在使用 Https 的狀況下,會經過 SSL 加密而更加安全。 ### 登入的實作 可以分別透過 Get 與 Post 方法做看看登入。 #### 使用 Get 假設我們只是把帳號密碼顯示出來: ```php= <?php $account = $_GET["account"]; $password = $_GET["password"]; echo "Account: $account"; echo "<br>Password: $password"; ``` 當使用 Get 進行登入,就會將帳號密碼帶到網址內: https://localhost/login?account=aaa&password=bbb ![](https://i.imgur.com/VZaWJkw.png) 因此,光是在瀏覽紀錄中,就可以看見帳號密碼了。 也可以使用 Postman 測試看看: ![](https://i.imgur.com/dvqYqUg.png) #### 使用 Post 假設我們只是把帳號密碼顯示出來: ```php= <?php $account = $_POST["account"]; $password = $_POST["password"]; echo "Account: $account"; echo "<br>Password: $password"; ``` 而使用 Post 方法時,送出後會透過 http body 攜帶,就會較安全。 可以使用 JavaScript 進行請求: ![](https://i.imgur.com/rVSFEWp.png) 然後使用 F12 開發者工具看看結果: ![](https://i.imgur.com/gYkkzTM.png) ![](https://i.imgur.com/oBX7Q0m.jpg) 也可以使用 Postman 進行請求測試看看: ![](https://i.imgur.com/z3GXlaZ.png) ## 我現在知道內容不同、方法不同了。那如果是資料,我要怎麼呈現? 有一個非常多地方都在用的一個資料格式,你一定要會,它叫做 JSON。 ### JSON 是什麼? - 資料格式:類似 String、int,更像 PHP 中的 Object。 - 自由、輕量、易讀、易寫。 ![](https://i.imgur.com/z1cCHWP.png) 在 PHP 中,大家可以透過 `json_encode(資料)` 的方式把變數轉換成 JSON。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully