前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >magento soap api

magento soap api

作者头像
全栈程序员站长
发布2022-09-14 10:11:31
1.7K0
发布2022-09-14 10:11:31
举报
文章被收录于专栏:全栈程序员必看

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

SOAP: simple object access protocol;

WSDL: webservice description language;

Magento Soap V1

代码语言:javascript
复制
v1 扩展案例

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

代码语言:javascript
复制
<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. 如下:

代码语言:javascript
复制
<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 添加权限描述

代码语言:javascript
复制
<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元素

代码语言:javascript
复制
<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 代码

代码语言:javascript
复制
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) { 
   
    }
}
代码语言:javascript
复制
V2 扩展案例
代码语言:javascript
复制
<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 方法的调用形式

代码语言:javascript
复制
$params = array(array(
    'status'=>array('eq'=>'pending'),
    'customer_is_guest'=>array('eq'=>'1'))
));
$result = $client->call($sessionId, 'sales_order.list', $params);

v2 方法的调用方式

代码语言:javascript
复制
$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 调用案例

代码语言:javascript
复制
$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 中,参数形式写成如下:

代码语言:javascript
复制
$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.cn/158605.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
网站建设
网站建设(Website Design Service,WDS),是帮助您快速搭建企业网站的服务。通过自助模板建站工具及专业设计服务,无需了解代码技术,即可自由拖拽模块,可视化完成网站管理。全功能管理后台操作方便,一次更新,数据多端同步,省时省心。使用网站建设服务,您无需维持技术和设计师团队,即可快速实现网站上线,达到企业数字化转型的目的。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档