如何使用SOAP V2为Magento设置自定义api


How to setup custom api for Magento with SOAP V2?

我在Magento 1.4.1.1上,我正在尝试使用SOAP v2在API中设置自定义函数。我让它为SOAP v1工作,但我需要v2以便c#可以使用它。对于v2,函数显示在WSDL中,但总是返回此错误:过程'testFoo'不存在。

这是我的文件:

/app/etc/模块/ABT_Test.xml

<?xml version="1.0"?>
<config>
    <modules>
        <ABT_Test>
            <active>true</active>
            <codePool>local</codePool>
        </ABT_Test>
    </modules>
</config>

/app/代码/地方/ABT/测试/etc/config . xml

<?xml version="1.0"?>
<config>
    <modules>
        <ABT_Test>
            <active>true</active>
            <codePool>local</codePool>
            <version>1.0</version>
        </ABT_Test>
    </modules>
    <global>
        <models>
            <test>
                <class>ABT_Test_Model</class>
            </test>
        </models>
    </global>
</config>

/app/代码/地方/ABT/测试/etc/api.xml

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <test>
                <model>test/api</model>
                <title>ABT Test Api</title>
                <methods>
                    <foo translate="title" module="test">
                        <title>Foo Test</title>
                        <method>foo</method>
                        <acl>test/foo</acl>
                    </foo>
                </methods>
            </test>
        </resources>
        <v2>
            <resources_function_prefix>
                <test>test</test>
            </resources_function_prefix>
        </v2>
    </api>
</config>

/app/代码/地方/ABT/测试/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/" />
        </schema>
    </types>
    <message name="testFooRequest">
        <part name="sessionId" type="xsd:string" />
    </message>
    <message name="testFooResponse">
        <part name="result" type="typens:boolean" />
    </message>
    <portType name="{{var wsdl.handler}}PortType">
        <operation name="testFoo">
            <documentation>Test Foo</documentation>
            <input message="typens:testFooRequest" />
            <output message="typens:testFooResponse" />
        </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="testFoo">
            <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>
</definitions>

/app/代码/地方/ABT/测试/模型/API.php

<?php
class ABT_Test_Model_Api extends Mage_Api_Model_Resource_Abstract
{
    public function foo()
    {
        return true;
    }
}
?>

/app/代码/地方/ABT/测试/模型/API/V2.php

<?php
class ABT_Test_Model_Api_V2 extends ABT_Test_Model_Api
{
}
?>
下面是我用来测试API的代码:
<?php
    $mageUser   = '########';
    $mageApiKey = '########';
    //SOAP V1
    echo "SOAP V1 <br />";
    $mageUrl    = 'http://www.########.com/api/soap/?wsdl';
    $soap = new SoapClient($mageUrl, array('cache_wsdl' => 0));
    try {
        $sessionID = $soap->login($mageUser, $mageApiKey);
        var_dump($soap->call($sessionID, 'test.foo', array()));
    } catch (Exception $e) {
        echo 'Exception: ' . $e->getMessage() . '<br />';
    }
    //SOAP V2
    echo "SOAP V2 <br />";
    $mageUrl2    = 'http://www.########.com/api/v2_soap/?wsdl';
    $soap2 = new SoapClient($mageUrl2, array('cache_wsdl' => 0));
    try {
        $sessionID2 = $soap2->login($mageUser, $mageApiKey);
        var_dump($soap2->testFoo($sessionID2));
    } catch (Exception $e) {
        echo 'Exception: ' . $e->getMessage() . '<br />';
    }
?>

我模糊了用户名、密码和url。函数显示在v2 WSDL中,php代码识别出它在WSDL中,但我仍然得到错误:过程'testFoo'不存在。

我错过了什么?

编辑:我按照Zyava的建议做了,它让我的例子起作用了。然后,我复制了文件夹,并做了一个精确的(区分大小写)查找和替换,以使用有意义的模块名称和函数名称。我小心翼翼地选择了一些我认为不会是含蓄的字眼。在新模块上,对v1 WSDL的调用工作正常,但v2给出相同的"Procedure 'xxx' not present"消息。然后,我将测试中的方法从"Foo"重命名为"Fooz",我得到了这个消息:"资源路径不可调用。"我发现有趣的是,我得到了不同的信息。这让我相信有一些缓存/配置/导致问题的东西。什么好主意吗?

首先我要提醒您Magento目前不支持SOAP v2格式,api/v2_soap/?WSDL只是soap api的第二个版本。

1。

<models>
    <test>
        <class>ABT_Test_Model</class>
    </test>
</models>

因为你写的不是核心模块,你应该写<abt_test>

2。<model>test/api</model>。你的情况应该是<model>abt_test/api</model>

3。<acl>test/foo</acl>

您的adminhtml.xml中是否存在此acl部分?

bygrace,

你的代码太完美了!

添加
....
  <resources_alias>
      <test>test</test>
  </resources_alias>
.....

<resources> and <v2>

on Api.xml

它会运行良好

我有同样的问题,我试图清除tmp文件,因为我使用xampp托管了它,它不起作用,我得到Procedure ххх not present exception,我不得不禁用缓存管理在服务器的管理页面
如何在管理页面禁用缓存
在系统下的管理页面中有一个名为缓存管理的部分,在开发阶段禁用一切,否则您所做的任何更改都不会反映在wsdl.