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商店集成。您还可以构建一个网络应用程序,您的员工可以使用它来帮助客户进行购买。在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);

?>

执行此代码(包含相关信息)后,您将看到类似于以下内容的输出:

时间: 2024-07-30 03:21:35

Magento 2 REST API入门的相关文章

Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)

不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce.ParseTVDataCompressAndCounter; import java.net.URI; import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Co

GoogleMap API 入门 —— 画图和线条

画图和线条分别用Polygon和Polyline,过程如下 1.定义一个画图信息相关的对象,这个对象画图和画线条都是可以应用的 1 var polyOption = 2 { 3 paths: [], 4 strokeColor: "#FF0000", 5 strokeOpacity: 0.8, 6 strokeWeight: 2, 7 fillColor: "#FF0000", 8 fillOpacity: 0.35 9 } 2.画多边形,这里有两种画法 可以先se

Web API 入门指南 - 闲话安全

参考页面: http://www.yuanjiaocheng.net/Spring/first.html http://www.yuanjiaocheng.net/entity/modelbrowser.html http://www.yuanjiaocheng.net/entity/dbcontext.html http://www.yuanjiaocheng.net/mvc/first.html http://www.yuanjiaocheng.net/webapi/first.html W

GoogleMap API 入门 —— 加载

1.引入GoogleMap js包 可以选择联网 <script type="text/javascript" src="http://ditu.google.cn/maps/api/js?sensor=false"></script> 也可以选择下载,然后本地运行 <script type="text/javascript" src="googleMap.js?sensor=false">

SQLite3 C语言API入门

下载SQLite3 我们下载sqlite源码包,只需要其中的sqlite3.c.sqlite.h即可. 最简单的一个创建表操作 #include <stdio.h>#include "sqlite3.h"int main(int argc,char *argv[]){    const char *sql_create_table="create table t(id int primary key,msg varchar(128))";    char

HBase编程 API入门系列之delete(管理端而言)(9)

大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门之delete(客户端而言) 就知道,在这篇博文里,我是在客户端里删除HBase表的. 这里,我带领大家,学习更高级的,因为,在开发中,尽量不能客户端上删除表. 所以,在管理端来删除HBase表.采用线程池的方式(也是生产开发里首推的) package zhouls.bigdata.HbaseProject.Pool; import java.io.IOException;import java.util.concurrent.E

HBase编程 API入门之create(管理端而言)

大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门之put(客户端而言) 就知道,在这篇博文里,我是在HBase Shell里创建HBase表的. 这里,我带领大家,学习更高级的,因为,在开发中,尽量不能去服务器上创建表. 所以,在管理端来创建HBase表.采用线程池的方式(也是生产开发里首推的) package zhouls.bigdata.HbaseProject.Pool; import java.io.IOException;import java.util.concur

HBase编程 API入门之delete

前面的基础,是 HBase编程 API入门之put HBase编程 API入门之get hbase(main):001:0> scan 'test_table2'ROW COLUMN+CELL row_04 column=f1:name, timestamp=1478117286377, value=Andy3 row_04 column=f2:name, timestamp=1478117286377, value=Andy3 1 row(s) in 1.5670 seconds hbase(

HBase编程 API入门之put

[[email protected] conf]$ cat regionservers HadoopMasterHadoopSlave1HadoopSlave2 <configuration> <property> <name>hbase.zookeeper.quorum</name> <value>HadoopMaster,HadoopSlave1,HadoopSlave2</value> </property> <