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)

Author's headshot

Christopher Davis

Christopher Davis has written this article. More details coming soon.

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

Stay in touch

Bringing news to you

Subscribe to our newsletter

By clicking and subscribing, you agree to our Terms of Service and Privacy Policy


Related Content

thumbnail image

AlliPMG CultureCampaigns & Client WorkCompany NewsDigital MarketingData & Technology

PMG Innovation Challenge Inspires New Alli Technology Solutions

4 MINUTES READ | November 2, 2021

thumbnail image

Applying Function Options to Domain Entities in Go

11 MINUTES READ | October 21, 2019

thumbnail image

My Experience Teaching Through Jupyter Notebooks

4 MINUTES READ | September 21, 2019

thumbnail image

Working with an Automation Mindset

5 MINUTES READ | August 22, 2019

thumbnail image

3 Tips for Showing Value in the Tech You Build

5 MINUTES READ | April 24, 2019

thumbnail image

Testing React

13 MINUTES READ | March 12, 2019

thumbnail image

A Beginner’s Experience with Terraform

4 MINUTES READ | December 20, 2018

ALL POSTS