Browse Site

Using the Magento API via php SOAP

The Magento API has a lot going for it, and the documentation is pretty good too (at The functionality that the API offers you outside of the usual scope of the site is immense, to the point that you could probably administer your site away from the admin web pages that you get with it. Assuming that you have the correct permissions to the service you can write data back as well as receive it. Through this you could in theory run the site off desktop software if you wanted to, combine it with other Magento or non-Magento sites to work with a centralised stock system  … pretty much anything that you wanted to, to seamlessly and easily run a shopping cart remotely. That is, once you figure out how exactly to pass your parameters correctly.

I’ve recently implemented a solution to pull customer and product data out of a Magento shopping cart using php’s SOAP client ( The lessons I’ve learnt include;

  • If you get the Magento call right but your parameters wrong, it ignores your parameters and returns everything available to it (which is frustrating until you realise this)
  • Field names / parameters are case-sensitive


Connecting to Magento

The first thing that you obviously need to do is to create the initial connection to the SOAP service.

$client = new SoapClient(SOAP_RESOURCE, array('proxy_host' => PROXY_SERVER);

If you’re connecting through a proxy in your development environment, you need to add further parameters:

$client = new SoapClient(SOAP_RESOURCE, array('proxy_host' => PROXY_SERVER,
 'proxy_port' => PROXY_PORT,
 'proxy_login' => NETWORK_USER,
 'proxy_password' => NETWORK_PWD));

Then authorise yourself on the service:

$session = $client->login(SOAP_USER, SOAP_PWD);


Customer Data

To pull back all customers simply call:

$client->call($session, 'customer.list');

What you get back is an array of data (assuming, in the above example, that only one customer is in your database):

0 =>
  'customer_id' => string '1' (length=1)
  'created_at' => string '2011-11-10 09:30:33' (length=19)
  'updated_at' => string '2011-11-14 13:53:32' (length=19)
  'increment_id' => string '' (length=0)
  'store_id' => string '1' (length=1)
  'website_id' => string '1' (length=1)
  'created_in' => string 'Site' (length=4)
  'firstname' => string 'Joe' (length=3)
  'lastname' => string 'Bloggs' (length=6)
  'email' => string '' (length=24)
  'group_id' => string '1' (length=1)
  'password_hash' => string '…' (length=…)

If you have the need to get information on individual users then you can simply call and use the customer ID as an identifier. If you don’t have this, though, you’ll need to filter your customer.list call with a third argument as below. Something that the API doesn’t appear to mention is that parameters are case insensitive, which for obvious reasons is handy to know!

$client->call($session, 'customer.list', array(array('firstname'=>'Joe', ‘lastname’=>’Bloggs’)));

The reason that your third parameter is an array within an array is because of the structure of the soap call – everything has to be bundled into a single array. If you need to pass multiple arguments (as opposed to the single argument of filtered fields in the example above) then they need to be within a single entity. This may appear obvious, as it does to me now, but at the time it caught me out when retrieving the list of products…


Product Data

To pull back individual product data simply call:

$product_attributes = array('product_id', 'sku', 'name', 'updated_at', 'price');
$client->call($session, '', array('product_id' => PRODUCT_ID, '', $product_attributes);

The parameters here are product ID / SKU, storeView (which is optional and I’m not taking into account but have to acknowledge anyway with an empty string) and the list of product fields.

  'product_id' => string '1' (length=1)
  'sku' => string 'product_1' (length=9)
  'set' => string '1' (length=1)
  'type' => string 'simple' (length=6)
  'categories' =>
    0 => string '5' (length=1)
    1 => string '7' (length=1)
    2 => string '42' (length=2)
  'websites' =>
    0 => string '1' (length=1)
  'name' => string 'Product One' (length=11)
  'updated_at' => string '2011-11-17 10:28:11' (length=19)
  'price' => string '149.9900' (length=8)

Something to note here is that regardless of the field names that you request, you always receive back the fields set, type, categories and websites. Usually this wouldn’t be a problem if it wasn’t for the fact that I was running a foreach and putting this result straight into a function that required a string. A handy time to use the ‘unset’ function.

The format of all calls through php’s SOAP functionality is very similar so at this point it’s simply a case of finding out what you need to get back and putting in the right call and parameters.

This entry was posted in Geeky Stuff, php.

Comments are closed.