This website uses cookies to ensure you get the best possible experience. See our Cookies Policy.

PMG Digital Made for Humans

Interacting with the Magento Web Services API (via Python)

3 MINUTE READ | November 1, 2011

Interacting with the Magento Web Services API (via Python)

The hardest part of any SEO’s job is extracting information about a product/page from the database or that product page itself. That’s why I covered parsing HTML/XML with Python in my last article. You can get a lot of stuff from scraping a page, but sometimes that’s not enough. Sometimes you need more information that isn’t directly available.

PMG has several current clients on Magento. And while Magento has a lot of stuff wrong with it, it does have a decent web services API that can help you get more info on just about any model in the Magento database. This is handy if you don’t have access to the database directly, or you want a faster way to retrieve data than web scraping. The Magento web services api comes in two flavors: SOAP and XML-RPC. We’ll be using XML-RPC here.

Python provides an easy to use library to interact with XML-RPC servers.

First off we need to import a few things. You can test all this from the Python interpreter, as the code in this tutorial will do.

First we need to import xmlrpclib and then we’ll set up a XML-RPC client by creating a new instance of xmlrpclib.ServerProxy.

shell> python
Python 2.7.1 [....]
>>> import xmlrpclib
>>> server = xmlrpclib.ServerProxy(‘http://www.yourmagentosite.com/api/xmlrpc/’)

If you need to see what methods are available to you, you can call server.system.listMethods() to see what’s available. If you need help with a method, call server.system.methodHelp('method_name').

>>> server.system.listMethods()
[‘system.listMethods’, ‘system.methodHelp’, ‘system.methodSignature’, ‘system.multicall’, ‘handlePhpError’, ‘startSession’, ‘endSession’, ‘login’, ‘call’, ‘multiCall’, ‘resources’, ‘resourceFaults’, ‘globalFaults’]
>>> server.system.methodHelp(‘login’)
‘Login user and Retrieve session id’

Before we can go any further, Magento will want us to log in. Be sure to enable webservices by visiting your admin area, then in the system menu, choose web services. First, create a role that has all the privledges you need. Then create a new user with that role. Be sure to remember your username and password!Next we need to call the server.login method. The first argument will be a username, and the second your password. This method returns a session key (a string of random letters and numbers. We need this later, so we’ll store it in a variable called session.

>>> session = server.login(‘your_username’, ‘your_password’)

he server.call method is the main interface to interact with magento. It will take three arguments: your session key, an API call, and the arguments you wish to send to that API call

So, let’s query a category. We’ll use the API call catalog_category.info, which requires one argument be send along: a category ID. You’ll get back a dictionary. Also worth noting is that the arguments being sent need to be an iterable.

>>> server.call(session, ‘catalog_category.info’, [‘151’])
{‘meta_keywords’: ”, ‘image’: ”, ‘children_count’: ‘0’, ‘updated_at’: ‘2011-10-28 16:15:39’, ‘url_key’: ‘jeans’, ‘children’: ”, ‘custom_design’: ”, ‘meta_description’: “Visit Example.com to see our entire selection of men’s denim, designer jeans and more!”, ‘increment_id’: None, ‘custom_layout_update’: ”, ‘page_layout’: ”, ‘all_children’: ‘151’, ‘parent_id’: 147, ‘custom_design_to’: None, ‘display_mode’: ‘PRODUCTS’, ‘meta_title’: ‘Mens Denim & Designer Jeans : Mens Fashion | Example.com’, ‘description’: ”, ‘custom_design_from’: None, ‘is_active’: ‘1’, ‘default_sort_by’: None, ‘include_in_menu’: ‘1’, ‘path’: ‘1/2/185/147/151’, ‘url_path’: ‘mens/clothing/jeans.html’, ‘is_anchor’: ‘1’, ‘landing_page’: None, ‘custom_apply_to_products’: ‘1’, ‘name’: ‘Jeans’, ‘level’: ‘4’, ‘created_at’: ‘2011-01-11 22:23:09’, ‘path_in_store’: None, ‘custom_use_parent_settings’: ‘1’, ‘position’: ‘6’, ‘category_id’: ‘151’, ‘filter_price_range’: None, ‘available_sort_by’: ”}

When you’re done, you need to invalidate your session key by calling server.endSession, which takes one argument: a session key.

>>> server.endSession(session)
True

Insights meet inbox

Sign up for weekly articles & resources.


Posted by Christopher Davis

Related Content

thumbnail image

Get Informed

PMG Innovation Challenge Inspires New Alli Technology Solutions

4 MINUTES READ | November 2, 2021

Get Informed

Applying Function Options to Domain Entities in Go

11 MINUTES READ | October 21, 2019

thumbnail image

Get Informed

My Experience Teaching Through Jupyter Notebooks

4 MINUTES READ | September 21, 2019

Get Informed

Trading Symfony’s Form Component for Data Transfer Objects

8 MINUTES READ | September 3, 2019

Get Inspired

Working with an Automation Mindset

5 MINUTES READ | August 22, 2019

Get Informed

Parsing Redshift Logs to Understand Data Usage

7 MINUTES READ | May 6, 2019

Get Inspired

3 Tips for Showing Value in the Tech You Build

5 MINUTES READ | April 24, 2019

thumbnail image

Get Informed

Testing React

13 MINUTES READ | March 12, 2019

Get Inspired

Tips for Designing & Testing Software Without a UX Specialist

4 MINUTES READ | March 6, 2019

Get Informed

A Beginner’s Experience with Terraform

4 MINUTES READ | December 20, 2018

ALL POSTS