我在Magento 1.4.1.1上,我尝试在使用SOAP v2的API中设置自定义函数。我有它为SOAP v1工作,但我需要V2,以便C#可以使用它。对于v2,该函数显示在WSDL中,但alwasy返回此错误:过程'testFoo'不存在。如何使用SOAP V2为Magento设置自定义api?
这里是我的文件:
/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
/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',然后我得到以下消息:“资源路径不可调用。”我觉得有趣的是,我得到了不同的信息。这导致我相信有一些缓存/配置/导致问题的东西。有任何想法吗?
对不起,你建议做的工作,但我有缓存问题。问题是服务器上的PHP WSDL缓存。我必须补充一点: '的ini_set( 'soap.wsdl_cache_enabled', '0');' – bygrace
@Zyava我不明白,为什么要使用 abt_test/API 而不是测试/ API ?由于“测试”已被定义为ABT_Test_Model。为什么仍然需要包含包名? –
ivantedja
对于非核心模块来说,在任何地方都可以使用完整模块名称(包名)。 – Zyava