I'm looking for a working example of a Magento API-enabled module. How can I define it, write the code for it, and call it?
A working config (in app/code/local/ModuleName/etc/ . I wrote mine into 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>
The PHP code (in app/code/local/ModuleName/Model/Api.php):
class ModuleName_Model_Api extends Mage_Api_Model_Resource_Abstract
{
function info()
{
return 'xxx';
}
}
The PHP code to actually call the SOAP interface:
$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()));
The result of running said script:
C:/Temp>php magesoap.php
string(3) "xxx"
Some notes:
o Error: "Invalid api path."
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.
Dustin Oprea
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With