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

这篇学习帖子来源:https://code.tutsplus.com/tutorials/create-a-custom-api-in-magento-part-one–cms-23785

介绍

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

为您的资源公开API在许多方面都是有益的。其中最明显的一点是,它使您的资源可以在不同的平台上使用,从而使它们与平台无关,这要归功于XML-RPC / SOAP等协议,它允许您以简化的方式公开资源。我们将使用SOAP作为自定义模块。

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

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

  • app/etc/modules/Envato_All.xml: It’s a file used to enable our custom module.
  • app/code/local/Envato/Customapimodule/etc/config.xml: It’s a module configuration file.
  • app/code/local/Envato/Customapimodule/etc/api.xml: It’s a file which declares the APIs provided by our module.
  • app/code/local/Envato/Customapimodule/etc/wsdl.xml: In this file, we’ll define the API methods as per the conventions of WSDL.
  • app/code/local/Envato/Customapimodule/Helper/Data.php: It’s a file used by the Magento translation system.
  • app/code/local/Envato/Customapimodule/Model/Product/Api.php: It’s a model file which implements the logic of our API methods.
  • app/code/local/Envato/Customapimodule/Model/Product/Api/V2.php: It’s a file to support Magento’s 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惯例宣布了“模型”和“助手”类。

继续,创建“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”,  它将提供产品列表。

此外,  <acl>标签下<customapimodule_product>用于为我们的资源提供访问控制。<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://local.magento1938.com/index.php/api/v2_soap?wsdl=1

页面时,您应该看到我们的方法“customapimoduleProductList”与其他API一起列出!

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

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注