zoukankan      html  css  js  c++  java
  • Magento WebServices SOAP API 创建和使用

    首先 SOAP 简介: http://baike.baidu.com/view/1695890.htm?fromtitle=SOAP

    然后简单介绍下Magento API。Magento API干啥用?我们可以通过该API进行App开发, 第三方对接等需要用到网站功能的第三方程序, 都可以使用Magento Api方便地交互。这里要介绍的就是SOAP API。

    访问权限:

    api的访问权限是通过后台设置配置role和user来决定的。也就是说, 对应一个api能否访问,看当前登录用户(WebServices 用户, 不是普通注册用户)是否有权访问。

    配置方法如下:

    在magento的管理后台 system>web services>soap....(如图)

    role为角色或者说是用户组管理, 每个用户组可以设置不同的权限。users为对应的web services用户管理,user的用户名和密码用于登录web services。可以为不同用户划分到不同的用户组。在用户组界面中可以管理web services api的访问权限, 如下图:

    我现在创建了以个用户组为myTest的组和一个用户名为jinko, 密码为123456的用户。此用户在调用web services的时候将会用到。

    下面就是创建一个自己的web services soap API。

    首先创建一个自己的模块(如果有自己的模块可以不用在创建),模块包名为Jinko, 模块名为Jinko_Test

    创建目录: 

    1 appcodelocalJinkoJdatahelper
    2 appcodelocalJinkoJdataetc
    3 appcodelocalJinkoJdatamodel

    在etc目录中新建一个config.xml文件输入如下代码:

     1 <!--
     2  此文件为当前模块的配置文件
     3 -->
     4 <config>
     5     <modules>
     6         <Jinko_Jdata><!--此标签名就是模块名-->
     7             <version>0.1.0</version><!--模块版本号-->
     8         </Jinko_Jdata>
     9     </modules>
    10     <frontend><!--前端-->
    11         <routers><!--路由-->
    12             <jdata><!--名为为jdata的路由-->
    13                 <use>standard</use>
    14                 <args>
    15                     <module>Jinko_Jdata</module><!--对应模块-->
    16                     <frontName>jdata</frontName><!--前端url路径使用名称-->
    17                 </args>
    18             </jdata>
    19         </routers>
    20     </frontend>
    21     <global><!--全局配置-->
    22         <helpers><!--定义辅助类工具-->
    23             <jdata><!--定义名称为jdata的一个辅助类-->
    24                 <class>Jinko_Jdata_Helper</class><!--此辅助工具对应的php类名-->
    25             </jdata>
    26         </helpers>
    27         <models><!--定义模型-->
    28             <jdata><!--可以把它想象成是一个文件夹名字-->
    29                 <rewrite><!--重写model名对应的类名-->
    30                     <data_api>Jinko_Jdata_Model_Data_Api</data_api><!--名称为data_api的model对应的类名-->
    31                 </rewrite>
    32             </jdata>
    33         </models>
    34     </global>
    35 </config>

    然后新建一个如下路径的xml文件:

    appetcmodulesJinko.xml

    写入如下代码, 告诉框架我做了个这样一个很屌的模块:

    1 <config>
    2     <modules>
    3         <Jinko_Jdata><!--模块名-->
    4             <active>true</active><!--激活-->
    5             <codePool>local</codePool><!--代码池-->
    6         </Jinko_Jdata>
    7     </modules>
    8 </config>

    这时候这个模块就建立好了。然后就是添加api配置,在如下新建一个如下路径的xml文件:

    1 appcodelocalJinkoJdataetcapi.xml

    输入如下配置代码:

     1 <config>
     2     <api><!--api 配置-->
     3         <resources><!--资源列表-->
     4             <jinkoapi_data translate="title" module="jdata"><!--api模块名称, 此名称用于soap client调用-->
     5                 <model>jdata/data_api</model><!--此api对应的处理model-->
     6                 <methods><!--此api模块有哪些调用的方法-->
     7                     <get translate="title" module="jdata"><!--get方法, 此方法名用于soap client调用-->
     8                         <acl>jinkoapi/data/get</acl><!--对应的权限是哪一项, 对应下面acl标签的设置-->
     9                         <method>getData</method><!--显示指定使用php类中的方法, 如不使用则默认为get(标签名, 注意的是, 部分php关键字不能作为php类的方法, 如list, array)-->
    10                     </get>
    11                     <set translate="title" module="jdata"><!--同理-->
    12                         <acl>jinkoapi/data/set</acl>
    13                         <method>setData</method>
    14                     </set>
    15                 </methods>
    16                 <faults module="jdata"><!--定义错误代码-->
    17                     <set_error><!--错误代码名称, 用于model类中使用指定返回此错误-->
    18                         <code>1001</code><!--错误代码-->
    19                         <message>set error!</message><!--错误消息-->
    20                     </set_error>
    21                     <no_access><!--同理-->
    22                         <code>1000</code>
    23                         <message>No access!</message>
    24                     </no_access>
    25                 </faults>
    26             </jinkoapi_data>
    27         </resources>
    28         <acl><!--此acl对应于admin页面role管理页面(用户组管理页面)中的权限复选框-->
    29             <resources>
    30                 <jinkoapi translate="title" module="jdata"><!--此标签名对应于上面acl标签的值-->
    31                     <title>JinkoApi</title><!--复选框名称-->
    32                     <data translate="title" module="jdata">
    33                         <title>Data</title><!--复选框名称, 此复选框上一级为jinkoapi-->
    34                         <get translate="title" module="jdata">
    35                             <title>Get Data</title><!--复选框名称, 此复选框上一级为data-->
    36                         </get>
    37                         <set translate="title" module="jdata"><!--同理-->
    38                             <title>Set Data</title>
    39                         </set>
    40                     </data>
    41                 </jinkoapi>
    42             </resources>
    43         </acl>
    44     </api>
    45 </config>

    上面配置文件中一些参数的对应关系如下图:

    现在api的配置已经写好了, 接下来就是实现这个api, 返回我们自己的东西。上面多了一个没有提到的东西, 就是helper,在此处不需要用到helper, 但是如果没有helper文件夹和默认的helper类, 在进入admin后台页面的web services role管理页面的时候会报错, 找不到类。这个Jinko_Jdata_helper类也是一个空类, 代码如下:

    1 <?php
    2 class Jinko_Jdata_Helper_Data extends Mage_Core_Helper_Abstract
    3 {
    4 
    5 }

    还有一点就是, 上图中还有一个文件时IndexController.php,这个文件是前端的一个控制器。跟API没有关系。

    接下来就是要实现api.php类,为什么这个文件的目录路径是这样的?:

    1 appcodelocalJinkoJdatamodeldataapi.php

    因为我们再congif.xml中指定的data_api模型的类名为Jinko_Jdata_Model_Data_Api。magento是根据类名来找文件的,则文件对应目录为jinko/jdata/model/data/api.php。这获取是约定的一个规则吧。

    api.php文件代码如下:

     1 <?php
     2 /**
     3  * Created by PhpStorm.
     4  * User: jingke.wu
     5  * Date: 2016/3/15
     6  * Time: 下午 05:25
     7  */
     8 class Jinko_Jdata_Model_Data_Api extends Mage_Api_Model_Resource_Abstract
     9 {
    10     public function getData()
    11     {
    12         @session_start();
    13         return array($_SESSION['jinko']);
    14     }
    15 
    16     public function setData($data)
    17     {
    18         @session_start();
    19 
    20         if($data == 'qqq') {
    21             $this->_fault('no_access');//对应api.xml的faults里的no_access标签
    22         }
    23 
    24         $_SESSION['jinko'] = array($data);
    25         return $this->getData();
    26     }
    27 }

    上面代码中, 主要来测试set方法, 在set方法(这里说的set方法是只web services调用时候的set, 对应上面类中的setData方法),传递的参数如果是字符串‘qqq’, 则返回一个没有访问权限的错误, 当然这个错误是我们自己编写的。如果不是qqq则返回一个数组, 并将参数打包进去。

    做到这一步, 还有一步重要的就是在后台webservices role配置那里,给新添加的api勾上权限, 如下图:

    好了, 现在已经基本就绪了。我们可以编写测试代码来测试了。

    测试代码随便写个1.php运行一下就可以看到结果了,测试代码1调用jinkoapi_data.set方法:

    1 <?php
    2 $client = new SoapClient('http://migb2c.com/api/soap?wsdl');
    3 $session = $client->login('jinko', '123456');
    4 $result = $client->call($session, 'jinkoapi_data.set', 'qqq');
    5 var_dump ($result);
    6 
    7 ?>

    注意代码的第三行, 调用soap的login方法的时候, 需要传递一个用户名和密码, 这个用户名和密码就是我一开始说的,在magento后台web services user中配置的, 而不是通过magento注册页面注册的用户, 也不是后台管理员admin的用户。然后调用call方法调用具体的模块方法。

    call方法的第一个参数是一个session_id, 也是login方法调用后的返回值. 第二个参数是对应web services的模块和方法, jinkoapi是我们再api.xml中定义的, set也是。·qqq·是传递的参数

    这个传递的参数是字符串的qqq, 那么根据我们写的程序其返回结果会是一个自定义的错误,如下图:

    从上图可以看到, 错误代码是1000, 错误消息是No access! 这些都是我们自己定义的。

    接下来的测试代码如下:

    1 <?php
    2 $client = new SoapClient('http://migb2c.com/api/soap?wsdl');
    3 $session = $client->login('jinko', '123456');
    4 $result = $client->call($session, 'jinkoapi_data.set', 'asasdasd');
    5 var_dump ($result);
    6 
    7 ?>

    这次的结果截图如下:

    正确返回数值。

    ok, 我们自己一个api然后测试调用。magento其实已经定义了很多个api可以直接供我们使用, 其调用方法也是一样的。具体有哪些模块方法可以现成调用呢?

    如下:

      1                     Catalog
      2                             Catalog Category
      3                                 catalog_category.assignedProducts 
      4                                 catalog_category.assignProduct 
      5                                 catalog_category.create 
      6                                 catalog_category.currentStore 
      7                                 catalog_category.delete 
      8                                 catalog_category.info 
      9                                 catalog_category.level 
     10                                 catalog_category.move 
     11                                 catalog_category.removeProduct 
     12                                 catalog_category.tree 
     13                                 catalog_category.update 
     14                                 catalog_category.updateProduct 
     15                             
     16                         
     17                         
     18                             Catalog Category Attributes
     19                                 catalog_category_attribute.currentStore 
     20                                 catalog_category_attribute.list 
     21                                 catalog_category_attribute.options 
     22                             
     23                         
     24                         
     25                             Catalog Product
     26                                 catalog_product.create 
     27                                 catalog_product.currentStore 
     28                                 catalog_product.delete 
     29                                 catalog_product.getSpecialPrice 
     30                                 catalog_product.info 
     31                                 catalog_product.list 
     32                                 catalog_product.listOfAdditionalAttributes 
     33                                 catalog_product.setSpecialPrice 
     34                                 catalog_product.update 
     35                             
     36                         
     37                         
     38                             Catalog Product Attribute
     39                                 product_attribute.addOption 
     40                                 product_attribute.create 
     41                                 product_attribute.currentStore 
     42                                 product_attribute.info 
     43                                 product_attribute.list 
     44                                 product_attribute.options 
     45                                 product_attribute.remove 
     46                                 product_attribute.removeOption 
     47                                 product_attribute.types 
     48                                 product_attribute.update 
     49                             
     50                         
     51                         
     52                             Catalog Product AttributeMedia
     53                                 catalog_product_attribute_media.create 
     54                                 catalog_product_attribute_media.currentStore 
     55                                 catalog_product_attribute_media.info 
     56                                 catalog_product_attribute_media.list 
     57                                 catalog_product_attribute_media.remove 
     58                                 catalog_product_attribute_media.types 
     59                                 catalog_product_attribute_media.update 
     60                             
     61                         
     62                         
     63                             Catalog Product Attribute Set
     64                                 product_attribute_set.attributeAdd 
     65                                 product_attribute_set.attributeRemove 
     66                                 product_attribute_set.create 
     67                                 product_attribute_set.groupAdd 
     68                                 product_attribute_set.groupRemove 
     69                                 product_attribute_set.groupRename 
     70                                 product_attribute_set.list 
     71                                 product_attribute_set.remove 
     72                             
     73                         
     74                         
     75                             Catalog Product CustomOption
     76                                 product_custom_option.add 
     77                                 product_custom_option.info 
     78                                 product_custom_option.list 
     79                                 product_custom_option.remove 
     80                                 product_custom_option.types 
     81                                 product_custom_option.update 
     82                             
     83                         
     84                         
     85                             Catalog Product Custom Option Value
     86                                 product_custom_option_value.add 
     87                                 product_custom_option_value.info 
     88                                 product_custom_option_value.list 
     89                                 product_custom_option_value.remove 
     90                                 product_custom_option_value.update 
     91                             
     92                         
     93                         
     94                             Catalog Product Downloadable Link
     95                                 product_downloadable_link.add 
     96                                 product_downloadable_link.list 
     97                                 product_downloadable_link.remove 
     98                             
     99                         
    100                         
    101                             Catalog Product Link
    102                                 catalog_product_link.assign 
    103                                 catalog_product_link.attributes 
    104                                 catalog_product_link.list 
    105                                 catalog_product_link.remove 
    106                                 catalog_product_link.types 
    107                                 catalog_product_link.update 
    108                             
    109                         
    110                         
    111                             Catalog Product Tag
    112                                 product_tag.add 
    113                                 product_tag.info 
    114                                 product_tag.list 
    115                                 product_tag.remove 
    116                                 product_tag.update 
    117                             
    118                         
    119                         
    120                             Catalog Product Tier Price
    121                                 catalog_product_attribute_tier_price.info 
    122                                 catalog_product_attribute_tier_price.update 
    123                             
    124                         
    125                         
    126                             Catalog Product Types
    127                                 catalog_product_type.list 
    128                             
    129                         
    130                     
    131                 
    132                 
    133                     Catalog Inventory
    134                         cataloginventory_stock_item.list 
    135                         cataloginventory_stock_item.update 
    136                     
    137                 
    138                 
    139                     Checkout    
    140                             Cart
    141                                  cart.create 
    142                                 cart.info 
    143                                  cart.license 
    144                                 cart.order 
    145                                  cart.totals 
    146                             
    147                         
    148                         
    149                             Cart Coupon
    150                                 cart_coupon.add 
    151                                 cart_coupon.remove 
    152                             
    153                         
    154                         
    155                             Cart Customer
    156                                 cart_customer.addresses 
    157                                 cart_customer.set 
    158                             
    159                         
    160                         
    161                             Cart Payment
    162                                 cart_payment.list 
    163                                 cart_payment.method 
    164                             
    165                         
    166                         
    167                             Cart Product
    168                                 cart_product.add 
    169                                 cart_product.list 
    170                                 cart_product.moveToCustomerQuote 
    171                                 cart_product.remove 
    172                                 cart_product.update 
    173                             
    174                         
    175                         
    176                             Cart Shipping
    177                                 cart_shipping.list 
    178                                 cart_shipping.method 
    179                             
    180                         
    181 
    182                     Customer
    183                         customer_group 
    184                         customer.create 
    185                         customer.delete 
    186                         customer.info 
    187                         customer.list 
    188                         customer.update 
    189                         
    190                             Customer Address
    191                                 customer_address.create 
    192                                 customer_address.delete 
    193                                 customer_address.info 
    194                                 customer_address.list 
    195                                 customer_address.update 
    196                             
    197                         
    198                     
    199                 
    200                 
    201                     Directory
    202                         directory_country.list 
    203                         directory_region.list 
    204                     
    205                 
    206                 
    207                     Sales
    208                             Sales Order
    209                                 sales_order.addComment 
    210                                 sales_order.cancel 
    211                                 sales_order.hold 
    212                                 sales_order.info 
    213                                 sales_order.list 
    214                                 sales_order.unhold 
    215                             
    216                         
    217                         
    218                             Sales Order Credit Memo
    219                                 sales_order_creditmemo.addComment 
    220                                 sales_order_creditmemo.cancel 
    221                                 sales_order_creditmemo.create 
    222                                 sales_order_creditmemo.info 
    223                                 sales_order_creditmemo.list 
    224                             
    225                         
    226                         
    227                             Sales Order Invoice
    228                                 sales_order_invoice.addComment 
    229                                 sales_order_invoice.cancel 
    230                                 sales_order_invoice.capture 
    231                                 sales_order_invoice.create 
    232                                 sales_order_invoice.info 
    233                                 sales_order_invoice.list 
    234                             
    235                         
    236                         
    237                             Sales Order Shipment
    238                                 sales_order_shipment.addComment 
    239                                 sales_order_shipment.addTrack 
    240                                 sales_order_shipment.create 
    241                                 sales_order_shipment.getCarriers 
    242                                 sales_order_shipment.info 
    243                                 sales_order_shipment.list 
    244                                 sales_order_shipment.removeTrack 
    245                             
    246                         
    247                     
    248                 
    249                 
    250                     Enterprise Customer Balance
    251                     
    252                     
    253                         
    254                             Customer Balance
    255                                 storecredit.balance 
    256                                 storecredit.history 
    257                             
    258                         
    259                         
    260                             Shopping Cart Customer Balance
    261                                 storecredit_quote.removeAmount 
    262                                 storecredit_quote.setAmount 
    263                             
    264                         
    265                     
    266                 
    267                 
    268                     Enterprise Gift Card
    269                     
    270                     
    271                         
    272                             Cart Gift Card
    273                                 cart_giftcard.add 
    274                                 cart_giftcard.list 
    275                                 cart_giftcard.remove 
    276                             
    277                         
    278                         
    279                             Gift Card Account
    280                                 giftcard_account.create 
    281                                 giftcard_account.info 
    282                                 giftcard_account.list 
    283                                 giftcard_account.remove 
    284                                 giftcard_account.update 
    285                             
    286                         
    287                         
    288                             Gift Card Customer
    289                                 giftcard_customer.info 
    290                                 giftcard_customer.redeem 
    291                             
    292                         
    293                     
    294                 
    295                 
    296                     Enterprise Gift Message
    297                         giftmessage.setForQuote 
    298                         giftmessage.setForQuoteItem 
    299                         giftmessage.setForQuoteProduct 
    300                     
    301                 
    302                 
    303                     Miscellaneous
    304                         magento.info 
    305                         store.info 
    306                         store.list 
    View Code

    具体作用及使用方法可以参见官网文档:http://devdocs.magento.com/guides/m1x/api/soap/introduction.html

  • 相关阅读:
    Python 3基础教程32-正则
    Python 3基础教程31-urllib模块
    Python 3基础教程30-sys模块
    Python 3基础教程29-os模块
    Python 3基础教程28-内置函数
    Python 3基础教程27-字典
    Python 3基础教程26-多行打印
    Python 3基础教程24-读取csv文件
    Python 3基础教程25-异常处理
    Python 3基础教程23-多维列表
  • 原文地址:https://www.cnblogs.com/JinkoWu/p/5284230.html
Copyright © 2011-2022 走看看