简体   繁体   English

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

[英]How to setup custom api for Magento with SOAP V2?

I am on Magento 1.4.1.1 and I am trying to setup a custom function in the API using SOAP v2. 我在Magento 1.4.1.1上,我正在尝试使用SOAP v2在API中设置自定义函数。 I have it working for SOAP v1 but I need v2 so that C# can use it. 我有它为SOAP v1工作,但我需要v2,以便C#可以使用它。 For v2 the function shows up in the WSDL but alwasy returns this error: Procedure 'testFoo' not present. 对于v2,函数显示在WSDL中,但alwasy返回此错误:过程'testFoo'不存在。

Here are my files: 这是我的文件:

/app/etc/modules/ABT_Test.xml /app/etc/modules/ABT_Test.xml

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

/app/code/local/ABT/Test/etc/config.xml /app/code/local/ABT/Test/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/code/local/ABT/Test/etc/api.xml /app/code/local/ABT/Test/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/code/local/ABT/Test/etc/wsdl.xml /app/code/local/ABT/Test/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/code/local/ABT/Test/Model/API.php /app/code/local/ABT/Test/Model/API.php

<?php
class ABT_Test_Model_Api extends Mage_Api_Model_Resource_Abstract
{

    public function foo()
    {
        return true;
    }
}
?>

/app/code/local/ABT/Test/Model/API/V2.php /app/code/local/ABT/Test/Model/API/V2.php

<?php
class ABT_Test_Model_Api_V2 extends ABT_Test_Model_Api
{
}
?>

And here is the code I use to test the 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 />';
    }
?>

I obscured the username, password and url. 我模糊了用户名,密码和网址。 The function shows up in the v2 WSDL and the php code recognizes that it is in the WSDL but I still get the error: Procedure 'testFoo' not present. 该函数显示在v2 WSDL中,并且php代码识别出它在WSDL中,但我仍然得到错误:过程'testFoo'不存在。

So what am I missing? 那我错过了什么?

EDIT: I did what Zyava suggested and it got my example working. 编辑:我做了Zyava建议的,它让我的例子工作。 I then copied the folder and did an exact (case sensitive) find and replace to use a meaningful Module name and function name. 然后我复制了该文件夹,并进行了精确(区分大小写)的查找和替换,以使用有意义的模块名称和函数名称。 I was careful to pick names that I didn't think would be reserve words. 我小心翼翼地选择我认为不会保留的名字。 On the new module the call on the v1 WSDL works fine but the v2 gives the same "Procedure 'xxx' not present" message. 在新模块上,对v1 WSDL的调用工作正常,但v2给出了相同的“Procedure'xxx'not present”消息。 I then went and renamed the method on the test from 'Foo' to 'Fooz' and I got this message: "Resource path is not callable." 然后,我将测试的方法从'Foo'重命名为'Fooz',我得到了这样的信息:“资源路径不可调用。” I find it interesting that I get a different message. 我觉得有趣的是我收到了不同的信息。 This leads me to believe there is some cache/configuration/something that is causing the problem. 这让我相信存在一些导致问题的缓存/配置/东西。 Any ideas? 有任何想法吗?

At first I should warn you that Magento doesn't support SOAP v2 format for now, api/v2_soap/?wsdl is just second version of soap api . 起初我应该警告你,Magento现在不支持SOAP v2格式,api / v2_soap /?wsdl只是soap api的第二个版本

1. 1。

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

Because you are writing module which isn't core, you should write <abt_test> 因为您正在编写非核心模块,所以应该编写<abt_test>

2. <model>test/api</model> . 2. <model>test/api</model> Should be <model>abt_test/api</model> in your case. 你的情况应该是<model>abt_test/api</model>

3. <acl>test/foo</acl> . 3. <acl>test/foo</acl>

Does this acl section exist in your adminhtml.xml? 这个acl部分是否存在于您的adminhtml.xml中?

bygrace, bygrace,

your code is so perfect! 你的代码是如此完美!

add

 .... <resources_alias> <test>test</test> </resources_alias> ..... 

same level with 与...相同的水平

<resources> and <v2>

on Api.xml 在Api.xml上

and it'll be working good. 它会很好用。

I has having the same issue and i tried clearing the tmp file as i had hosted it using xampp it didnt work i was getting Procedure ххх not present exception , I had to disable the cache management in the admin page of the server . 我有同样的问题,我尝试清除tmp文件,因为我使用xampp托管它没有工作我得到Procedure ххх not present exception ,我不得不在服务器的管理页面中禁用缓存管理
How to disable Cache in Admin page 如何在管理页面中禁用缓存
In the admin page under the system there is a section called cache management disable everything during the development phase or else any change you do wont be reflected in the wsdl. 在系统下的管理页面中,有一个名为缓存管理的部分在开发阶段禁用所有内容,否则您所做的任何更改都不会反映在wsdl中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM