magento soap api

magento soap apiSOAP:simpleobjectaccessprotocol;WSDL:webservicedescriptionlanguage;MagentoSoapV1v1扩展案例step1:在etc下新建api.xml,内容如下

大家好,又见面了,我是你们的朋友全栈君。

SOAP: simple object access protocol;

WSDL: webservice description language;

Magento Soap V1

v1 扩展案例

step 1: 在 etc 下 新建 api.xml,内容如下

<config>
    <api>
        <resources>
        </resources>
        <acl>
            <resources>
                <all>
                </all>
            </resources>
        </acl>
    </api>
</config>

step 2: 添加一个资源信息(模块名,不要加namespace)
注意:在etc 下的XML文件中,不要使用namespace, 否则会报错,会把当前模块下的helper 去Mage下查找。
在 resource 下添加 method,method 中的元素有 list ,create,update,delete.info. 如下:

<config>
  <api>
    ....
  <resources>
     <customer translate="title" module="customer">
       <title>Customer Resource</title>
         <methods>
         <list translate="title"module="customer">
           <title>Retrive customers</title>
         </list>
        <create translate="title"module="customer">
         <title>Create customer</title>
       </create>
       <info translate="title" module="customer">
        <title>Retrieve customer data</title>
       </info>
       <update translate="title" module="customer">
         <title>Update customer data</title>
      </update>
      <delete>
        <title>Delete customer</title>
      </delete>
   </methods>
   <faults module="customer">
   </faults>
 </customer>
</resources>
....
 </api>
</config> 

其中的 faults 是报错的信息。

step3 添加权限描述

<config>
<api>
....
  <acl>
   <resources>
     <customer translate="title" module="customer">
        <title>Customers</title>
        <list translate="title" module="customer">
            <title>View All</title>
        </list>
        <create translate="title" module="customer">
           <title>Create</title>
        </create>
        <info translate="title" module="customer">
          <title>Get Info</title>
        </info>
        <update translate="title" module="customer">
          <title>Update</title>
       </update>
       <delete translate="title" module="customer">
          <title>Delete</title>
      </delete>
   </customer>
 </resources>
</acl>
</api>
</config>

step 4 匹配ACL资源和API资源方法,通过添加ACL元素

<config>
    <api>
      <resources>
        <customer translate="title" module="customer">
            <title>Customer Resource</title>
            <acl>customer</acl>
            <methods>
             <list translate="title" module="customer">
                <title>Retrive customers</title>
                <acl>customer/list</acl>
                <method>items</method>
             </list>
           <create translate="title" module="customer">
                <title>Create customer</title>
                <acl>customer/create</acl>
            </create>
            <info translate="title" module="customer">
                 <title>Retrieve customer data</title>
                  <acl>customer/info</acl>
             </info>
           <update translate="title" module="customer">
                 <title>Update customer data</title>
                 <acl>customer/update</acl>
            </update>
         <delete translate="title" module="customer">
                <title>Delete customer</title>
                <acl>customer/delete</acl>
            </delete>
       </methods>
              ....
  </customer>
   </resources>
....
    </api>
</config>

由于list是PHP关键字,因此用items代替list

step 5 创建PHP API 代码

class Mage_Customer_Model_Customer_Api extends Mage_Api_Model_Resource_Abstract { 
   

    public function create($customerData) { 
   
    }

    public function info($customerId) { 
   
    }

    public function items($filters) { 
   
    }

    public function update($customerId, $customerData) { 
   
    }

    public function delete($customerId) { 
   
    }
}
V2 扩展案例
<v2>
   <resources_function_prefix>
    <customer>customerCustomer</customer>
       <customer_group>customerGroup</customer_group>
       <customer_address>customerAddress</customer_address>
    </resources_function_prefix>
</v2>

加在<api>标签中

v2 PHP 代码

这里写图片描述

v1 和v2 的区别在于
v1 方法的调用形式

$params = array(array(
    'status'=>array('eq'=>'pending'),
    'customer_is_guest'=>array('eq'=>'1'))
));
$result = $client->call($sessionId, 'sales_order.list', $params);

v2 方法的调用方式

$params = array('filter' => array(
    array('key' => 'status', 'value' => 'pending'),
    array('key' => 'customer_is_guest', 'value' => '1')
));
$result = $client->salesOrderList($sessionId, $params);

salesOrder 就是定义的方法前缀名。

v1 URL http://magentohost/api/soap/?wsdl
v2 URL http://magentohost/api/v2_soap?wsdl=1

WSDL对于soap v1 和 soap v2 是不同的。v1 自定义开发的模块,并不需要修改 wsdl.xml 文件,v2 则需要修改 wsdl.xml 文件

magento 后台配置
system -> webservice ->soap roles ,soap user
其中在 soap user中, username 和 new api key 是获得session id的重要数据

PHP soap 方法client 调用案例

$api_url_v1 = "http://host/api/soap/?wsdl=1";

$username = 'username';
$password = 'passord';
try {

$cli = new SoapClient($api_url_v1);
//retreive session id from login
$session_id = $cli->login($username,$password);
//call customer.list method
$result = $cli->call($session_id, 'catalog_product.list',array($params));
}catch (SoapFault $fault){
    var_dump($fault->faultstring);
}

print_r($result);

在client call 中,由于有的服务端需要 通过 参数的key值来获取value,所以,以php client call 中,参数形式写成如下:

$param=array('key'=>value,'key1'=>value1)

注意事项:
vagrant+virtualbox 环境中,
必须保持 vagrantifile 中 guest 和host 的端口号一致,并且登录到虚拟机中,配置 /etc/hosts 文件,加入一行
127.0.0.1 local.example.com。
负责访问 local.example.com:post/api/soap/?wsdl 会报错如下:
Couldn’t load from ‘http://local.fenxiao.com:8081/api/soap/?wsdl’ : failed to load external entity “http://local.fenxiao.com:8081/api/soap/?wsdl

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/158605.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • MyBatis-延迟加载与MyBatis缓存(面试题)

    MyBatis-延迟加载与MyBatis缓存(面试题)MyBatis-延迟加载与MyBatis缓存-概念性MyBatis-延迟加载与MyBatis缓存MyBatis-延迟加载与MyBatis缓存-概念性延迟加载(面试题)1、什么是延迟加载(按需加载)2、延迟加载MyBatis缓存(面试题)1、Cache缓存2、MyBatis缓存分析3、一级缓存4、二级缓存原理开启二级缓存5、禁用二级缓存6、刷新二级缓存延迟加载(面试题)1、什么是延迟加载(按需…

  • 冯·诺依曼计算机特点[通俗易懂]

    冯·诺依曼计算机特点冯·诺依曼,20世纪最重要的数学家之一。在现代计算机、博弈论、核武器和生化武器等众多领域内有杰出建树的最伟大的科学全才之一,被后人称为“计算机之父”和“博弈论之父”。一、冯·诺依曼计算机结构二、冯·诺依曼计算机的特点计算机由五大部件组成:存储器,运算器,控制器,输入设备,输出设备。指令和数据以同等地位存于存储器,可按地址顺序访问。指令和数据用二进制表示。指令由操作码和地址码组成。存储程序,程序在计算机中顺序存放。以运算器为中心。(不合理:花大量的时间进行数据传输,降

  • 怎么修改HTML网页的名字_如何修改html文件内容

    怎么修改HTML网页的名字_如何修改html文件内容NetCms默认设置中,只能上传Doc文件,不能上传xls文件和PPT文件。 上传文件类型可以“控制面板–>参数设置–>上传文件允许格式”中设置。但是,仅能上传,添加新闻时,添加附件的文件选择框中无法看到xls文件和ppt文件。 通过查看源文件,添加新闻页面是~/Manage/News/News_add.aspx文件,在该文件中,添加附件位置,通过调用JavaScript的s

  • Html5 拖放上传图片

    Html5 拖放上传图片

  • c++与三菱plc通信_C语言编写的代码是

    c++与三菱plc通信_C语言编写的代码是FX系列作为三菱基本款的PLC,它们之间的通讯体例别离如下:CC-LINK,N:N网络连接,并联连接。1.CC-LINK连接CC-LINK连接图如下:三菱PLC通讯与编程实例!-1.jpg(21.71KB,下载次数:0)2018-6-103:01上传对应的PLC可为FX1N、FX1NC、FX2N、FX2NC、FX3U、FX3UC,因为在使用CC-LINK通讯时要扩展CC-LINK模块,…

  • Windows server 2003 安装vs2005 sp1补丁包报1718错误的解决方法

    Windows server 2003 安装vs2005 sp1补丁包报1718错误的解决方法收藏于2012-03-30迁移自个人的百度空间——————————–解决步骤如下1.在控制面板中打开“管理工具“。2.双击“本地安全策略”。 3.单击“软件限制策略”。(注意:如果未列出软件限制,请右击“软件限制策略”,然后单击“新建策略”。)4.在“对象类型”下,双击“强制”。 5.单击“除本地管理员以外的所有用户”…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号