Retrieving Product Attributes from Magento's V2 API
Monday, July 12. 2010
I've been working with the API for Magento in recent weeks and I had a bit of a struggle explaining to the V2 API which attributes of a product I wanted to retrieve. Actually I had issues talking to the V2 API at all, but that's a different post so I'll skate over those for now. Instead I thought I'd share (or rather, record for the next time I have the same problem!) how to specify which attributes about a product to retrieve.
It actually wasn't complicated but without V2 API documentation, it wasn't at all clear what to feed in to get the result I was looking for. It turns out you can just pass an array of desired attributes, shown here with the info method from the product_catalog:
// connect to soap server
$client = new SoapClient('http://magentoinstall.local/api/v2_soap?wsdl=1');
// log in
$session = $client->login('user', 'pass');
// product info
$attributes = new stdclass();
$attributes->attributes = array('product_title', 'description', 'short_description', 'price');
$list = $client->catalogProductInfo($session, <sku>, NULL, $attributes);
There were two tricks - one was realising that I could pass that final (undocumented) argument, and the other was understanding how to format that. Hopefully anyone doing battle with the same thing will find this post and get over this little challenge much faster than I did :)
It actually wasn't complicated but without V2 API documentation, it wasn't at all clear what to feed in to get the result I was looking for. It turns out you can just pass an array of desired attributes, shown here with the info method from the product_catalog:
// connect to soap server
$client = new SoapClient('http://magentoinstall.local/api/v2_soap?wsdl=1');
// log in
$session = $client->login('user', 'pass');
// product info
$attributes = new stdclass();
$attributes->attributes = array('product_title', 'description', 'short_description', 'price');
$list = $client->catalogProductInfo($session, <sku>, NULL, $attributes);
There were two tricks - one was realising that I could pass that final (undocumented) argument, and the other was understanding how to format that. Hopefully anyone doing battle with the same thing will find this post and get over this little challenge much faster than I did :)
Accessing the Magento V2 API
Thursday, June 24. 2010
Recently I've been working with Magento at work, and in particular with integrating with their API. Now, before I say anything more, I must say that I am always pleased when I see that these products do include some kind of API. The Magento one is a bit interesting, although there is some half-decent API documentation for the original API.
However they have then released a new version of the API, with very little documentation. So here are two calls - one to the v1 API and one to the v2 - which I hope will help illustrate the differences. The example I'll give is the customer list functionality, including filtering the result set - because this was a total mystery when I started working with the v2 API!
$options = array(
"location" => 'http://magentoinstall.local/index.php/api/index/index/',
"uri" => 'http://magentoinstall.local/api/'
);
$client = new SoapClient(NULL, $options);
$session = $client->login('user', 'pass');
$list = $client->call($session, 'customer.list', array(array("customer_id" => "42")));
To make the same call with API version 2, we need to address the method in a different way, using the structure in the underlying code as the method name that we call, and CamelCasing those, like this:
$client = new SoapClient('http:/magentoinstall.local/api/v2_soap?wsdl=1');
$session = $client->login('user', 'pass');
$filter = new StdClass();
$filter->filter = array(array("key" => "customer_id", "value" => "42"));
$list = $client->customerCustomerList($session, $filter);
I haven't used either of the APIs a lot but once I was able to call the same method via both available services, I wanted to share the approach here in the hope that this would help someone else trying to solve the same problem. It is certainly not obvious from the documentation how to interact with the v2 API and I had some real puzzles getting the filtering working. These snippets are from my working code so I hope they are helpful to someone!
However they have then released a new version of the API, with very little documentation. So here are two calls - one to the v1 API and one to the v2 - which I hope will help illustrate the differences. The example I'll give is the customer list functionality, including filtering the result set - because this was a total mystery when I started working with the v2 API!
$options = array(
"location" => 'http://magentoinstall.local/index.php/api/index/index/',
"uri" => 'http://magentoinstall.local/api/'
);
$client = new SoapClient(NULL, $options);
$session = $client->login('user', 'pass');
$list = $client->call($session, 'customer.list', array(array("customer_id" => "42")));
To make the same call with API version 2, we need to address the method in a different way, using the structure
$client = new SoapClient('http:/magentoinstall.local/api/v2_soap?wsdl=1');
$session = $client->login('user', 'pass');
$filter = new StdClass();
$filter->filter = array(array("key" => "customer_id", "value" => "42"));
$list = $client->customerCustomerList($session, $filter);
I haven't used either of the APIs a lot but once I was able to call the same method via both available services, I wanted to share the approach here in the hope that this would help someone else trying to solve the same problem. It is certainly not obvious from the documentation how to interact with the v2 API and I had some real puzzles getting the filtering working. These snippets are from my working code so I hope they are helpful to someone!
Web Services Tutorial on TechPortal
Tuesday, June 1. 2010
I'm very pleased to say that today I have a a post about web services for PHP developers published on techPortal! OK so I edit techPortal so this is the written equivalent of introducing myself as a speaker but I enjoyed writing the post and I hope it'll be a useful overview for PHP developers looking at web services and working with them.
Accessing the Magento Web API
Tuesday, May 4. 2010
I've been working with the Magento Web API lately, and the first problem I ran into was actually getting access to it. Contrary to its reputation, I found some perfectly good documentation outlining how to connect to the service and use it. I thought I was on to a winner but I kept seeing:
Further investigation led me to this forum post - web services are separate users and you must first set them up through the admin screens - and make sure also to allocate roles to them.
The slight pitfall at this point is that you create a username and an API key - these then become the apiUser and apiKey variables mentioned in the documentation. The key is basically a password, its starred out in the settings and you have to enter it twice. Now I know that, I can log in to my service! Hope this helps someone else get to the point faster than I did.
Fatal error: Uncaught SoapFault exception: [2] Access denied.
Further investigation led me to this forum post - web services are separate users and you must first set them up through the admin screens - and make sure also to allocate roles to them.
The slight pitfall at this point is that you create a username and an API key - these then become the apiUser and apiKey variables mentioned in the documentation. The key is basically a password, its starred out in the settings and you have to enter it twice. Now I know that, I can log in to my service! Hope this helps someone else get to the point faster than I did.
GETting RESTful collections - may I filter?
Tuesday, March 23. 2010
At work at Ibuildings recently, I've been teaching some classes on web services, and its a topic that I've spoken about once or twice at conferences. But something has always bothered me, so I find myself in the unusual position of blogging a question.
So, when you are retrieving information from a RESTful service. You have two options: retrieve a specific resource, whose URL you know; or retrieve a collection, which may contain a list of resources. I've also seen some nice ways of filtering collections, by creating kind of "sub collections" or "views", similar to what twitter does with the URL of lists, for example http://twitter.com/dpcon/speakers10 which is like a filtered list of twitter users.
Is it RESTful to add GET parameters to a collection in order to add functionality such as filtering, sorting, or pagination? What I have in mind is a URL that looks something like this:
This is what I would do with a search results page in a web application, and I use the same approach to web services which works really well and I recommend it to everyone! But is it RESTful?
I am also wondering where OpenSearch would fit into the answer for all this, I only noticed it recently but the more I look at it the more I think it could be an interesting addition!
Thoughts, links for me to RTFM, and all other additions are welcome in the comments box :)
RESTful collections
So, when you are retrieving information from a RESTful service. You have two options: retrieve a specific resource, whose URL you know; or retrieve a collection, which may contain a list of resources. I've also seen some nice ways of filtering collections, by creating kind of "sub collections" or "views", similar to what twitter does with the URL of lists, for example http://twitter.com/dpcon/speakers10 which is like a filtered list of twitter users.
What if I want to search and sort?
Is it RESTful to add GET parameters to a collection in order to add functionality such as filtering, sorting, or pagination? What I have in mind is a URL that looks something like this:
- http://example.com/users?orderby=firstname&start=0
- http://example.com/users?start=0&limit=25
- http://example.com/users?active=1&orderby=join_date&limit=12
This is what I would do with a search results page in a web application, and I use the same approach to web services which works really well and I recommend it to everyone! But is it RESTful?
I am also wondering where OpenSearch would fit into the answer for all this, I only noticed it recently but the more I look at it the more I think it could be an interesting addition!
Thoughts, links for me to RTFM, and all other additions are welcome in the comments box :)
Supermondays: Recap
Tuesday, February 23. 2010
Last night I travelled to the northeast of England to speak to a thriving technical community up there called Supermondays. They contacted me some time ago asking if I could get there to speak one Monday, and last night was the night! It was a very civilised gathering, with sandwiches and cups of tea, and using a lecture theatre at the university for space. As a speaker the best thing about this is that its a space designed for addressing people in, unlike most user groups (and indeed conferences!) where two steps away from the lectern sees you standing in the dark, falling off the stage, or getting projected on to. Last night was a different story with lots of space to wander, slides projected well above me on the wall so everyone could see clearly, and relatively good acoustics despite no amplification.
My talk was entitled "PHP and Web Services: Perfect Partners" - the slides are on slideshare if you want to take a look. There was also a talk about android development by Alex Reid, including a live coding demo which went surprisingly well! Judging by the various events that were plugged and discussed on the night, at the main event and in the pub afterwards, this is a diverse and vibrant technical community - so if you are in the northeast, get along to Supermondays!
My talk was entitled "PHP and Web Services: Perfect Partners" - the slides are on slideshare if you want to take a look. There was also a talk about android development by Alex Reid, including a live coding demo which went surprisingly well! Judging by the various events that were plugged and discussed on the night, at the main event and in the pub afterwards, this is a diverse and vibrant technical community - so if you are in the northeast, get along to Supermondays!
(Page 1 of 4, totaling 22 entries)
» next page



Comments