Custom API (Example)

Last modified by dsoprea on Wed, April 13, 2011 12:14
Source|Old Revisions  

This is an old revision of the document!

A working config (in app/code/local/ModuleName/etc/ . I wrote mine into api.xml):

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

The PHP code (in app/code/local/ModuleName/Model/Api.php):

  1. <?php
  2.     class ModuleName_Model_Api extends Mage_Api_Model_Resource_Abstract
  3.     {
  4.         function info()
  5.         {
  6.             return 'xxx';
  7.         }
  8.     }

The PHP code to actually call the SOAP interface:

  1. <?php
  3. $mageUrl    = 'http:/local.magecomm/api/?wsdl';
  4. $mageUser   = 'soaptest';
  5. $mageApiKey = 'apitest';
  7. $soap = new SoapClient($mageUrl);
  9. $sessionID = $soap->login($mageUser, $mageApiKey );
  11. var_dump($soap->call($sessionID, '', array()));

The result of running said script:

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

Some notes:

  • Error: “Invalid api path.”
  • > This means that Magento can’t find the module.
  • 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) [<a href='function.include'>function.include</a>]: 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() [<a href='function.include'>function.include</a>]: 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
  • 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.
  • The <method> 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.
  • The <model> 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...).
  • I have not encountered a situation where the value of the “module” attributes (<... module=”“>) seem to matter.