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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JVM自动内存管理机制–读这篇就GO了

    之前看过JVM的相关知识,当时没有留下任何学习成果物,有些遗憾。这次重新复习了下,并通过博客来做下笔记(只能记录一部分,因为写博客真的很花时间),也给其他同行一些知识分享。Java自动内存管理机制包

    2022年2月16日
    38
  • 90后的我们越长大越孤单

    90后的我们越长大越孤单

    2021年8月9日
    52
  • WebService 实例应用

    WebService 实例应用两个工程分别部署在两台电脑上:webservice_client客户端  webservice_server:服务器端先说服务器导入jar包改写xml文件:cxfcom.rainspnsor.webservice.CXFNonSpringServiceImpl0cxf/services/*然后创建类:1util中:

    2022年7月21日
    15
  • Window 平台下添加 tree 命令[通俗易懂]

    Window 平台下添加 tree 命令[通俗易懂]在WIndow平台下,系统自带的命令行工具CMD可以使用dir命令来以树结构打印目录文件,Powershell工具可以使用tree命令。但是,一般为了开发方便,通常会使用更接近Linux命令的Git-Bash作为常用的命令行工具,然而Git-Bash却不支持tree命令。以下为给Window平台下Git-Bash添加tree命令的方法。tree获取路…

    2022年7月24日
    37
  • unity3d官网下载安装教程_3D怎么安装

    unity3d官网下载安装教程_3D怎么安装入门小菜鸟,希望像做笔记记录自己学的东西,也希望能帮助到同样入门的人,更希望大佬们帮忙纠错啦~侵权立删。一、官网下载链接https://unity3d.com/cn/但是可能是因为我没翻墙,所以我根本没办法进入这个网页二、Unity3D中国官网下载但是天无绝人之路嘛可不是,让我找到了这么个大宝贝Unity所有版本下载|Unity中国官网点进链接后往下翻然后根据提示进行一些注册和登录,就可以选择你想要的版本进行下载啦三、Unity3D安装教程这里我选择.

    2026年1月22日
    6
  • 使用jdbc连接mysql数据库_mysql允许远程连接

    使用jdbc连接mysql数据库_mysql允许远程连接JDBC(JavaDatabaseConnectivity,Java数据库连接)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。本文讲述如何使用JDBC来连接和访问数据库。为方便引入JDBC依赖包,我们创建Maven项目来实现我们的示例程序。打开IntelliJIDEA客户端,File-New-…

    2025年9月6日
    9

发表回复

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

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