在Magento中创建自定义API:第一部分

介绍

对于任何成功的平台,至关重要的是要紧跟最新功能并提供它们以在市场中竞争。 公开适用于您平台的API是允许与第三方系统集成的重要功能之一,从而为更大的社区打开了大门。 Magento是最成功的电子商务平台之一,提供了许多有用的功能和实用程序,证明了它是真正的企业级框架。

为您的资源公开API在许多方面都是有益的。 最明显的原因之一是,借助XML-RPC / SOAP之类的协议,它可以使资源在不同的平台上可用,从而使它们在平台上独立,这使您可以以简化的方式公开资源。 我们将使用SOAP作为自定义模块。

在本教程中,我们将创建一个自定义模块“ Customapimodule”。 我假设您熟悉Magento中的基本模块创建过程。 这是一篇很好的文章,解释了自定义模块创建的基础。

文件设置概览

以下是所需设置所需的文件列表:

  • app / etc / modules / Envato_All.xml:这是用于启用我们的自定义模块的文件。
  • app / code / local / Envato / Customapimodule / etc / config.xml:这是一个模块配置文件。
  • app / code / local / Envato / Customapimodule / etc / api.xml:这是一个声明我们模块提供的API的文件。
  • app / code / local / Envato / Customapimodule / etc / wsdl.xml:在此文件中,我们将根据WSDL的约定定义API方法。
  • app / code / local / Envato / Customapimodule / Helper / Data.php:这是Magento翻译系统使用的文件。
  • app / code / local / Envato / Customapimodule / Model / Product / Api.php:这是一个模型文件,可实现我们API方法的逻辑。
  • app / code / local / Envato / Customapimodule / Model / Product / Api / V2.php:这是支持Magento v2 API的文件。

自定义模块创建:设置文件

首先,我们将创建一个模块启动器文件。 创建一个文件“ app / etc / modules / Envato_All.xml”,并将以下内容粘贴到该文件中。 我们使用“ Envato”作为模块名称空间,使用“ Customapimodule”作为模块名称。 默认情况下,它将启用我们的“ Customapimodule”模块。

<?xml version="1.0"?>
<config>
  <modules>
    <Envato_Customapimodule>
      <active>true</active>
      <codePool>local</codePool>
    </Envato_Customapimodule>
  </modules>
</config>

接下来,我们需要创建一个模块配置文件。 创建“ app / code / local / Envato / Customapimodule / etc / config.xml”,并将以下内容粘贴到该文件中。

<?xml version="1.0"?>
<config>
  <modules>
    <Envato_Customapimodule>
      <version>1.0</version>
    </Envato_Customapimodule>
  </modules>
  <global>
    <models>
      <customapimodule>
        <class>Envato_Customapimodule_Model</class>
      </customapimodule>
    </models>
    <helpers>
      <customapimodule>
        <class>Envato_Customapimodule_Helper</class>
      </customapimodule>
    </helpers>
  </global>
</config>

这里没什么好想的-根据Magento约定,我们仅声明了“ Model”和“ Helper”类。

继续,创建“ app / code / local / Envato / Customapimodule / etc / api.xml”,并将以下内容粘贴到该文件中。 “ api.xml”文件用于声明模块公开的API方法。

<?xml version="1.0"?>
<config>
  <api>
    <resources>
      <customapimodule_product translate="title" module="customapimodule">
        <model>customapimodule/product_api</model>
        <title>Demo Custommoduleapi API</title>
        <acl>customapimodule/product</acl>
        <methods>
          <list translate="title" module="customapimodule">
            <title>List of products</title>
            <method>items</method>
          </list>
        </methods>
      </customapimodule_product>
    </resources>
    <resources_alias>
      <product>customapimodule_product</product>
    </resources_alias>
    <v2>
      <resources_function_prefix>
         <product>customapimoduleProduct</product>
      </resources_function_prefix>
    </v2>
    <acl>
      <resources>
        <customapimodule translate="title" module="customapimodule">
          <title>Products</title>
          <sort_order>5</sort_order>
          <product translate="title" module="customapimodule">
            <title>Product data</title>
          </product>
        </customapimodule>
      </resources>
    </acl>
  </api>
</config>

我们将从<resources>标记开始,该标记包装了模块声明的所有资源。 您可以将“资源”视为一个实体,您希望使用该实体对API方法进行分类。

在我们的示例中,我们只声明了一个名为<customapimodule_product>资源。 您可以随意命名,只要它是唯一的标识符即可。 在<customapimodule_product>资源标记下,我们声明了<model>标记以链接Magento“模型”文件,在此文件中我们将定义API方法定义。 我们资源的方法由<methods>标记包装。 在我们的案例中,我们仅在<list>标记下定义了一个方法“ items”,该方法将提供产品列表。

此外, <customapimodule_product>下的<acl>标记用于为我们的资源提供访问控制。 <acl>标记“ customapimodule / product”中定义的值引用文件底部的定义。

在文件底部,您可以看到我们已经声明了一个单独的<acl>标记,该标记定义了“ customapimodule / product”。 简而言之,它用于将我们的资源置于访问控制之下,因此,如果在Magento后端中以这种方式定义了某些“ API角色”,则可以访问这些资源。 我们将在本教程的下一部分中对此进行更详细的讨论。

目前,支持两个版本的Magento API,即v1和v2,您可以使用它们创建和公开API。 在我们的示例中,我们将看到两种方法。 <resources_alias>标记用于定义资源别名,通过该别名将调用我们的方法。 我们将其定义为<product> ,因此,每当您要使用Magento v1 API调用API方法时,都将使用“ product”作为资源前缀。 以相同的方式<v2>为Magento v2 API定义资源别名,因此资源前缀将为“ customapimoduleProduct”。 当我们在下一教程中看到如何调用API时,这些事情将变得更加清晰。

接下来,让我们创建“ app / code / local / Envato / Customapimodule / etc / wsdl.xml”文件,然后粘贴以下内容。

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:typens="urn:{{var wsdl.name}}" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
  name="{{var wsdl.name}}" targetNamespace="urn:{{var wsdl.name}}">
  <types>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Magento">
      <import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" />
      <complexType name="fieldInfo">
        <sequence>
          <element name="entity_id" type="xsd:string"/>
          <element name="name" type="xsd:string"/>
        </sequence>
      </complexType>
      <complexType name="fieldInfoArray">
        <complexContent>
          <restriction base="soapenc:Array">
            <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:fieldInfo[]" />
          </restriction>
        </complexContent>
      </complexType>
    </schema>
  </types>
  <message name="customapimoduleProductListRequest">
    <part name="sessionId" type="xsd:string" />
  </message>
  <message name="customapimoduleProductListResponse">
    <part name="products" type="typens:fieldInfoArray" />
  </message>
  <portType name="{{var wsdl.handler}}PortType">
    <operation name="customapimoduleProductList">
      <documentation>List of products</documentation>
      <input message="typens:customapimoduleProductListRequest" />
      <output message="typens:customapimoduleProductListResponse" />
    </operation>
  </portType>
  <binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
    <operation name="customapimoduleProductList">
      <soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
      <input>
        <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
      </input>
      <output>
        <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
      </output>
    </operation>
  </binding>
  <service name="{{var wsdl.name}}Service">
    <port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
      <soap:address location="{{var wsdl.url}}" />
    </port>
  </service>
</definitions>

“ wsdl.xml”文件用于根据SOAP语法定义API方法定义。 在本教程的上下文中,我们将在此文件中看到一些重要的标记。

首先,我们定义了“ fieldInfo”复杂类型,其中包含两个元素:“ entity_id”和“ name”。 此外,我们定义了“ fieldInfoArray”复杂类型,它是从“ fieldInfo”复杂类型派生的。 它是“ fieldInfo”复杂类型的数组。

简单来说,我们已经定义了对象属性,这些属性将在API方法调用的响应中返回。 就我们而言,我们将返回一系列产品。 数组的每个项目将具有两个属性:产品的“ entity_id”和“ name”。 您可以根据需要定义更多属性。

接下来,在<message>标记“ customapimoduleProductListRequest”下,我们使用<part>标记定义了所需的输入参数。 同样,在<message>标记“ customapimoduleProductListResponse”下,我们定义了输出对象的类型。 当我们调用API方法时,我们需要传递“ sessionId”,并且该API方法的响应将包含产品数组。 当您调用http:// yourmagentostore / api / v2_soap?wsdl = 1时,其余的标签将使我们的方法出现。

接下来,我们需要创建“ app / code / local / Envato / Customapimodule / Helper / Data.php”文件,以确保Magento的翻译系统正常运行。 它几乎是一个空文件,但应按照约定放在那里!

<?php
class Envato_Customapimodule_Helper_Data extends Mage_Core_Helper_Abstract
{
}

接下来,让我们创建一个模型文件“ app / code / local / Envato / Customapimodule / Model / Product / Api.php”。

<?php
// app/code/local/Envato/Customapimodule/Model/Product/Api.php
class Envato_Customapimodule_Model_Product_Api extends Mage_Api_Model_Resource_Abstract
{
  public function items()
  {
    $arr_products=array();
    $products=Mage::getModel("catalog/product")
      ->getCollection()
      ->addAttributeToSelect('*')
      ->setOrder('entity_id', 'DESC')
      ->setPageSize(5);

    foreach ($products as $product) {
      $arr_products[] = $product->toArray(array('entity_id', 'name'));
    }

    return $arr_products;
  }
}

回想一下,在前面的“ api.xml”中,我们定义了一个由<list>标记包装的“ items”方法。 因此,在上面的模型类中,我们刚刚实现了这一点。

在这种方法中,我们仅获取五个最新产品,并遍历每个项目以准备具有属性“ entity_id”和“ name”的产品数组。 因此,现在您可能应该了解在“ wsdl.xml”中创建“复杂类型”的原因!

此外,我们还需要创建一个模型文件来支持Magento v2 API。 让我们创建一个包含以下内容的模型文件“ app / code / local / Envato / Customapimodule / Model / Product / Api / v2.php”。

<?php
//app/code/local/Envato/Customapimodule/Model/Product/Api/V2.php
class Envato_Customapimodule_Model_Product_Api_V2 extends Envato_Customapimodule_Model_Product_Api
{
}

如您所见,它只是扩展了先前在“ app / code / local / Envato / Customapimodule / Model / Product / Api.php”文件中定义的模型类。

就自定义API实现而言,就文件设置而言就是这样。 如果您感到好奇,请从后端启用模块并清除缓存。 现在,当您访问http:// yourmagentostore / api / v2_soap?wsdl = 1页面时,您应该会看到我们的方法“ customapimoduleProductList”与其他API一起列出了!

在下一部分中,我们将继续学习如何创建API用户和API角色,当然,以及如何使用本教程中定义的自定义API方法!

翻译自: https://code.tutsplus.com/tutorials/create-a-custom-api-in-magento-part-one--cms-23785

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值