Web Services: Additional information
This is an old revision of the document!
The basic info on Magento API through web services jumps past some important points. It took me some time to figure this out, so I’ll share some points here.
- Web services API is only available in Magento version 1.1.0 and later.
- To use it one should first install the Zend Framework (for XmlRpc). It can be downloaded from http://framework.zend.com/download. The Minimal version is enough.
- Copy the Zend directory to somewhere the webserver can see it and update the PHP variable ‘include_path’. On Linux, all include paths traversed by the php need to have x permission (for group and other).
Then some work in the Magento admin panel is required:
- A user for the API must be created in System > Web Services > Users
- The username and password is required for logging in from the client script (”apiuser” , “apipass”).
- A role for the API must be created in System > Web Services > Roles
- Assign permissions to the role: The simplest is to select “All” in the “Resource Access” selector.
- Bind the new user to the new role.
Then, a basic script using XmlRpc can be run from a client. My test shop is at “localhost/magento”. Let’s retrieve the customer list:
<?php require 'Zend/XmlRpc/Client.php'; $client = new Zend_XmlRpc_Client('http://localhost/magento/api/xmlrpc/'); // If somestuff requires api authentification, // we should get session token $session = $client->call('login', array('apiuser', 'apipass')); $r = $client->call('call', array($session, 'customer.list')); var_dump($r); // If you don't need the session anymore $client->call('endSession', array($session)); ?>
After a lot of trial and error I find the reason why I have problems getting SOAP calls to work:
* The first call was made to http://localhost/soaptest.php. * This script calls on a WSDL file at http://localhost/magento/api/?wsdl * This script generates a nested call to http://www.magento.se/magento/api/?wsdl... to request the actual WSDL
... and in having two URLs (which both point back to the local machine) PHP session handling breaks down. The solution was to use the same URL for anything that represents the same session:
* The first call was made to http://localhost/soaptest.php. * This script calls on a WSDL file at http://www.magento.se/magento/api/?wsdl * This script generates a nested call to http://www.magento.se/magento/api/?wsdl... to request the actual WSDL
Now it works!
On measuring performance on my local machine, it seems that SOAP is around 3 times slower than XmlRpc. The nested callback to render the WSDL is not really necessary, it will just go back to the server. It is quite time consuming.