• About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
  • About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
November 01, 2011

Interacting with the Magento Web Services API (via Python)

Posted by Christopher Davis

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.

Getting Started and Connecting

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.

Python
1
2
3
4
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').

Python
1
2
3
4
>>> 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’

Authenticating

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.

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

Getting Some Data!

The 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.

Python
1
2
>>> 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’: ”}

Finishing Up

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

Python
1
2
>>> server.endSession(session)
True

magentomagento web services apiPythonxml
Previous
Next

Latest White Papers

  • Shifting Plans for 2020 & Beyond
  • Game On: How Brands Can Log Into A Diverse Multi-Billion Dollar Industry
  • What CCPA Means For Brands
  • How Google is Improving Consumer Data Privacy
  • Ways to Prepare for the Cookieless Future
  • See all White Papers

Featured Posts

  • Ad Age Names PMG #1 Best Place to Work in 2021
  • MediaPost Names PMG Independent Agency of the Year
  • PMG Client Portfolio Trends During Amazon Prime Day 2020
  • A Closer Look at the Congressional Big Tech Market Power Report
  • What to Know About Reddit

Categories

  • Consumer Insights
  • Content
  • Creative Design
  • Data Analytics
  • Development
  • Digital TV & Video
  • Ecommerce
  • Industry News
  • Local
  • Mobile
  • Paid Search
  • PMG Culture
  • Programmatic & Display
  • SEO
  • Social Media
  • Structured Data
Fort Worth

2845 West 7th Street
Fort Worth, TX 76107

Dallas

3102 Oak Lawn Avenue
Suite 650
Dallas, TX 75219

Austin

823 Congress Avenue
Suite 800
Austin, TX 78701

London

33 Broadwick Street
London
W1F 0DQ

New York

120 East 23rd Street
New York, NY 10010

Get in touch

(817) 420 9970
info@pmg.com

Subscribe to the PMG Newsletter
© 2021 PMG Worldwide, LLC, All Rights Reserved
  • Contact
  • Privacy Policy
 Tweet
 Share
 Tweet
 Share
 Tweet
 Share
 LinkedIn
We and our partners use cookies to personalize content, analyze traffic, and deliver ads. By using our website, you agree to the use of cookies as described in our Cookie Policy.