Accessing the Magento V2 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
$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!
Nice job. I was wondering the same. Thanks for the kick start.
Hi Lorna,
Thanks for your post! I just started working recently w/Magento’s API… Just out of curiousity, what version of Magento are you working with?
Cheers!
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
Hi Lorna,
Great post that has helped me a lot. Your code works perfectly for selecting the customer by ‘customer_id’ but I cannot figure out for the life of me how to get a list of customers based on ‘updated_at’.
I have tried lots of variations of the following code:
[geshi lang=php]
$client = new SoapClient(‘http://mymagehost/index.php/api/v2_soap?wsdl=1’);
$session = $client->login(‘username’, ‘password’);
$filter = new StdClass();
$filter->filter = array(array(“key” => “updated_at”, “value” => array(“key”=>”from”, “value”=>”2011-01-25 00:00:00”)));
$list = $client->customerCustomerList($session, $filter);
[/geshi]
Can you shed some light on this for me?
Thanks in advance!
Dave
Hi Dave,
Have you got a ans for how to put filters, I’ve also tried many alternatives but not able to find the answer please let me know if you have answer for this on my mail id [email protected]
Dave: I’m sorry, I really can’t shed any more light on this. I’m not using magento any more so I don’t even have a testing platform to play with. Try asking your question on stackoverflow or a magento forum – and good luck finding an answer!
Thanks for writing this! I was also frustrated by the lack of documentation.
Hello,
Thank you for the illustration. I am currently working on the magneto v2 API in vb.net. Everything works fine but i cannot make out how to filter orderlist.
I would really be thankful if you can help me with this. I am kind of stuck over here
If still someone has a problem with that…. correct $filter should be:
[code]
$filter = array( ‘complex_filter’ => array( array( ‘key’ => ‘updated_at’, ‘value’ => array( ‘key’ => ‘from’, ‘value’ => ‘2011-01-25 00:00:00’ ) ) ) );
[/code]
Thank you madam, you saved the day :-)
Can someone please translate the complex filter syntax into pure XML for the soap request? This would be very helpful for anyone not using PHP for their API calls…