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

标签 c# php web-services magento soap

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

这是我的文件:

/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

<?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

<?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

<?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

<?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

<?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 />';
    }
?>

我隐藏了用户名、密码和网址。该函数出现在 v2 WSDL 中,并且 php 代码识别出它在 WSDL 中,但我仍然收到错误:过程“testFoo”不存在。

那我错过了什么?

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

最佳答案

首先我应该警告你,Magento 目前不支持 SOAP v2 格式,api/v2_soap/?wsdl 只是 second version of 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 部分?

关于c# - 如何使用 SOAP V2 为 Magento 设置自定义 api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7519556/

相关文章:

c# - 如何使 JIT 将堆栈变量扩展到范围末尾(GC 太快)

php - 使用PHP将CSV数据导入mySQL

php - 除 EC2 实例 AWS 上的 Laravel 5.1 之外的所有其他路由均出现 "Not Found"错误

web-services - 是否有可以可靠地安排在准确时间执行作业的云服务?

c# - 是否可以对函数应用程序和 Web 应用程序使用单个 Application Insights 实例

c# - 如何在 C# 中显示测试性能数字的准确时间

PHP Amazon SES v3 - 缺少必需的 header 'From'

Java:从 wsdl 获取示例请求 XML

web-services - 带空格的 Systemd 服务名称

c# - 为什么有些方法对可空结构的空值起作用而有些方法不起作用?