码迷,mamicode.com
首页 > Windows程序 > 详细

Magento 2 REST API入门

时间:2017-09-13 11:12:46      阅读:3150      评论:0      收藏:0      [点我收藏+]

标签:enc   ima   请求   gui   调用   管理员   应用   结果   字段   

Magento 2 API框架允许开发人员创建与Magento 2商店进行通信的新服务。它支持RESTSOAPWeb服务,并且基于CRUD操作(创建,读取,更新,删除)和搜索模型。

目前,Magento 2使用以下三种身份验证方法:

  • 用于第三方应用程序的OAuth 1.0a验证。
  • 令牌用于认证移动应用。
  • 管理员和客户身份验证与登录凭据。

这些验证方法只能访问分配给它们的资源。Magento 2 API框架首先检查呼叫是否具有执行请求的适当授权。API框架还支持API响应的现场过滤,以保持蜂窝带宽。

开发人员使用Magento 2 API进行广泛的任务。例如,您可以创建购物应用程序并将其与Magento 2商店集成。您还可以构建一个网络应用程序,您的员工可以使用它来帮助客户进行购买。在API的帮助下,您可以将Magento 2商店与CRM,ERP或POS系统集成。

在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服务角色,请按照下列步骤操作:

  1. 登录到Magento 2管理面板
  2. 转到系统 >> 用户角色并点击添加新角色
  3. 输入角色名称
  4. 您的密码字段中,输入您的Magento 2管理员的当前密码。
  5. 现在,在左侧,单击角色资源
  6. 资源访问中,仅选择Web服务所需的那些。
  7. 完成后,点击保存角色

在Magento 2中创建Web服务用户

现在,通过以下步骤为新创建的角色创建一个新用户:

  1. 转到系统 >> 所有用户并点击添加新用户
  2. 输入所需信息,包括用户名第一电子邮件密码等。
  3. 现在,在左侧,单击用户角色并选择新创建的角色。
  4. 完成后,单击保存用户

就这样; 现在我将使用该用户到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入门

标签:enc   ima   请求   gui   调用   管理员   应用   结果   字段   

原文地址:http://www.cnblogs.com/shenlongjue/p/7513839.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!