magento创建自定义api

比如自己的ModuleName为Test
1,创建api.xml(in app/code/local/CustomPackage/Test/etc/ )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CustomPackage_Test>
<version>0.1.0</version>
</CustomPackage_Test>
</modules>
<api>
<resources>
<test translate="title" module="test">
<model>CustomPackage_Test_Model_Api</model>
<title>Test Resource</title>
<acl>test</acl>
<methods>
<info translate="title" module="test">
<title>Retrieve test data</title>
<acl>test/info</acl>
<method>info</method>
</info>
</methods>
<faults module="test">
</faults>
</test>
</resources>
<acl>
<resources>
<test translate="title" module="test">
<title>tests</title>
<info translate="title" module="test">
<title>Get Info Test</title>
</info>
</test>
<all>
</all>
</resources>
</acl>
</api>
</config>

 

2,PHP 代码 (in app/code/local/CustomPackage/Test/Model/Api.php):

1
2
3
4
5
6
7
class CustomPackage_Test_Model_Api extends Mage_Api_Model_Resource_Abstract
{
function info()
{
return 'xxx';
}
}

 

3,soap测试

1
2
3
4
5
6
7
8
9
10
11
12
13
$mageUrl = 'http:/test.magento.com/api/?wsdl';
$mageUser = 'soaptest';
$mageApiKey = 'apitest';

$soap = new SoapClient($mageUrl);

$sessionID = $soap->login($mageUser, $mageApiKey);

var_dump($soap->call($sessionID, 'customer.info', array()));
The result of running said script:

C:/Temp>php magesoap.php
string(3) "xxx"

 

4,出现的一些错误
查看var/log/system.log文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
o Error: "Invalid api path."

This means that Magento can't find the module.
o Error: "Resource path is not callable."

This means that Magento can't call the method in the module.

You can use the system.log file to debug this. It will display one or more errors about how it couldn't autoload the requested class from the calculated file-path.

2011-04-13T15:15:24+00:00 DEBUG (7): include(Mage/Customer/Model/Api.php) [function.include]: failed to open stream: No such file or directoryC:/Development/Projects/MagentoCommercial/lib/Varien/Autoload.php

2011-04-13T15:15:24+00:00 DEBUG (7): include() [function.include]: Failed opening 'Mage/Customer/Model/Api.php' for inclusion (include_path='C:/Development/Projects/MagentoCommercial/app/code/local;C:/Development/Projects/MagentoCommercial/app/code/community;C:/Development/Projects/MagentoCommercial/app/code/core;C:/Development/Projects/MagentoCommercial/lib;.;C:/Development/Libraries;C:/Development/Libraries/Standard/_Pear')C:/Development/Projects/MagentoCommercial/lib/Varien/Autoload.php

o The module-config XML above will link this SOAP interface to a new item in the API permissions called "Get Info Test" under the "Customers" group. It will then be available to allow or deny on specific API users/roles.

o The value under /config/api/resources/customer/methods/info in the module-config XML is the internal method name of the method that should be bound to the SOAP resource-name. If they're the same, then you may omit this.

o The value under /config/api/resources/customer (which is 'ModuleName_Model_Api') is the full class-name here because it's obviously referring to my class, which isn't part of Mage. If you're trying to call an existing class within Mage, you can just use the shorthand notation (xxx/yyy, xxx/yyy_zzz, etc..).

o It's only in the ACL part of the module-config that the value of the "module" attributes (<... module="">) seems to matter. All the same, make sure it's set properly (case doesn't matter) everywhere. It might be that they just haven't implemented it mainstream, yet, and to ignore it will just cause you problems later.

 

坚持原创技术分享,您的支持将鼓励我继续创作!