Magento 2 REST API入門
Magento 2 API框架允許開發人員創建與Magento 2商店進行通信的新服務。它支持REST和SOAPWeb服務,並且基於CRUD操作(創建,讀取,更新,刪除)和搜索模型。
目前,Magento 2使用以下三種身份驗證方法:
- 用於第三方應用程序的OAuth 1.0a驗證。
- 令牌用於認證移動應用。
- 管理員和客戶身份驗證與登錄憑據。
這些驗證方法只能訪問分配給它們的資源。Magento 2 API框架首先檢查呼叫是否具有執行請求的適當授權。API框架還支持API響應的現場過濾,以保持蜂窩帶寬。
開發人員使用Magento 2 API進行廣泛的任務。例如,您可以創建購物應用程序並將其與Magento 2商店集成。
在Magento 2中使用REST API
如果要使用基於令牌的Magento 2 REST API,首先您將需要從Magento 2進行身份驗證並獲取令牌。然後,您必須將其傳遞給您執行的每個請求的標題。
要使用基於令牌的身份驗證在Magento 2中開始使用REST API,您將需要創建一個Web服務用戶角色,並將該角色註冊到新的Magento 2管理用戶。請記住,創建新角色和用戶是必要的,因為在Web服務中使用Magento Owner用戶並不是一個好習慣。
本教程的目的:使用此REST API來獲取Magento 2存儲上安裝的所有模塊。
在Magento 2中創建Web服務角色
要在Magento 2中創建Web服務角色,請按照下列步驟操作:
- 登錄到Magento 2管理面板。
- 轉到系統 >> 用戶角色並點擊添加新角色
- 輸入角色名稱。
- 在您的密碼字段中,輸入您的Magento 2管理員的當前密碼。
- 現在,在左側,單擊角色資源。
- 在資源訪問中,僅選擇Web服務所需的那些。
- 完成後,點擊保存角色
在Magento 2中創建Web服務用戶
現在,通過以下步驟為新創建的角色創建一個新用戶:
- 轉到系統 >> 所有用戶並點擊
- 輸入所需信息,包括用戶名,第一和姓,電子郵件,密碼等。
- 現在,在左側,單擊用戶角色並選擇新創建的角色。
- 完成後,單擊保存用戶
就這樣; 現在我將使用該用戶到Magento 2 REST API Web服務。
Magento 2 REST API驗證
如前所述,我將通過令牌認證驗證REST API。這意味著我將 在初始連接中傳遞 用戶名 和 密碼並接收 令牌 。該令牌將保存在一個變量中,該變量將在標題中傳遞以進一步調用。
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
$token = curl_exec($ch);
//decoding generated token and saving it in a variable
$token= json_decode($token);
?>
在上述代碼中,我 使用API URL傳遞 用戶名 和 密碼, 以驗證Magento 2 REST API,然後將該標記保存 在一個變量中供進一步使用。請記住,您必須使用新用戶的用戶名密碼(之前創建)。
在Magento 2中獲取使用REST API的模塊
您可以使用Magento 2 REST API獲取幾乎所有內容。該REST API,用於Magento的EE和CE名單這個主題很好的指導。
為了演示API,我將把所有安裝的模塊放在Magento 2商店上。這是腳本:
<?php
//Using above token into header
$headers = array("Authorization: Bearer ".$token); //API URL to get all Magento 2 modules $requestUrl=‘http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules‘; $ch = curl_init($requestUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); //decoding result $result= json_decode($result); //printing result print_r($result); ?>在上面的代碼中,我 使用API?? URL 在標頭中傳遞了令牌(前面提到的), 以獲得Magento 2存儲上安裝的所有模塊。
接下來,我將打印結果。這是完整的代碼:
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
$token = curl_exec($ch);
//decoding generated token and saving it in a variable
$token= json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl=‘http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules‘;
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result= json_decode($result);
//printing result
print_r($result);
?>
執行此代碼(包含相關信息)後,您將看到類似於以下內容的輸出:
Magento 2 REST API入門