It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




已关闭8年。





我正在寻找启用Magento API的模块的工作示例。如何定义它,编写代码并调用它?

最佳答案

一个有效的配置(在app / code / local / ModuleName / etc /中。我将其写入api.xml):

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <ModuleName>
            <version>0.1.0</version>
        </ModuleName>
    </modules>
    <api>
        <resources>
            <customer translate="title" module="xyz">
                <model>ModuleName_Model_Api</model>
                <title>Customer Resource</title>
                <acl>customer</acl>
                <methods>
                    <info translate="title" module="xyz">
                        <title>Retrieve customer data</title>
                        <acl>customer/info</acl>
                        <method>info</method>
                    </info>
                </methods>
                <faults module="xyz">
                </faults>
            </customer>
        </resources>
        <acl>
            <resources>
                <customer translate="title" module="xyz">
                    <title>Customers</title>
                    <info translate="title" module="xyz">
                        <title>Get Info Test</title>
                    </info>
                </customer>
                <all>
                </all>
            </resources>
        </acl>
    </api>
</config>


PHP代码(在app / code / local / ModuleName / Model / Api.php中):

class ModuleName_Model_Api extends Mage_Api_Model_Resource_Abstract
{
    function info()
    {
        return 'xxx';
    }
}


实际调用SOAP接口的PHP代码:

$mageUrl    = 'http:/local.magecomm/api/?wsdl';
$mageUser   = 'soaptest';
$mageApiKey = 'apitest';

$soap = new SoapClient($mageUrl);

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

var_dump($soap->call($sessionID, 'customer.info', array()));


运行上述脚本的结果:

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


一些注意事项:

o错误:“无效的api路径。”


这意味着Magento找不到模块。


o错误:“资源路径不可调用。”


这意味着Magento无法调用模块中的方法。
您可以使用system.log文件进行调试。它将显示一个或多个错误,说明如何无法从计算的文件路径自动加载请求的类。


2011-04-13T15:15:24 + 00:00调试(7):include(Mage / Customer / Model / Api.php)[function.include]:无法打开流:无此类文件或目录C:/ Development /项目/MagentoCommercial/lib/Varien/Autoload.php

2011-04-13T15:15:24 + 00:00调试(7):include()[function.include]:无法打开要包含的“ Mage / Customer / Model / Api.php”(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上面的module-config XML将此SOAP接口链接到“客户”组下名为“获取信息测试”的API权限中的新项目。然后,它将可用于允许或拒绝特定的API用户/角色。

o模块配置XML中的/ config / api / resources / customer / methods / info下的值是应绑定到SOAP资源名称的方法的内部方法名称。如果它们相同,那么您可以忽略它。

o / config / api / resources / customer下的值(即'ModuleName_Model_Api')在这里是完整的类名,因为它显然是指我的类,它不是Mage的一部分。如果您试图在Mage中调用现有的类,则可以使用简写形式(xxx / yyy,xxx / yyy_zzz等)。

o仅在module-config的ACL部分中,“ module”属性()的值似乎很重要。都一样,确保在任何地方都正确设置(大小写无关紧要)。可能是他们还没有将它实现为主流,而忽略它只会在以后给您带来麻烦。

达斯汀·奥普里亚(Dustin Oprea)

07-26 09:30
查看更多