Jump to content

Working with webservices


przemal

Recommended Posts

Hello everyone,

After few hours and many tries I finally figured out how to use webservices, so here's what I've found out:

http://dev.machine/prestashop/api/ - returns key(the one you are using to authenticate) permissions to tables (addresses, categories, products...), get="true" says you can get data, post="true" means you can add data.

Getting data is simple as opening for example http://dev.machine/prestashop/api/categories/ which lists shop categories. To get detailed information about let's say category with id 1, simply go to http://dev.machine/prestashop/api/categories/1.

Adding new data is a bit harder.
You have to send it as xml (with POST or PUT) very similar to the one you are receiving. From the source code it seems that you don't have to include all fields (only required) but I always got error 500(<![CDATA[internal error]]>) when I tried to omit some fields. (They can be empty but they have to be sent).

It is also important to set correct content type in http header and send it in a field called xml otherwise you'll get error 500.

Below I'm including example written in python to get data and add new category, as it's always easier to understand with code example.


#!/usr/bin/env python3

import httplib2
import urllib.parse
#httplib2.debuglevel = 1

key=''
url='http://dev.machine/prestashop/api/'


def get(resource):
   h = httplib2.Http(".cache")
   h.add_credentials(key, '')
   resp, content = h.request(url+resource, "GET")
   strContent = content.decode('utf-8')
   print(resp)
   print(strContent)

def add(resource):
   h = httplib2.Http(".cache")
   h.add_credentials(key, '')
   xml = open('category_new.xml', 'r')
   data = {'xml': xml.read()}
   xml.close();
   resp, content = h.request(url+resource, "POST", urllib.parse.urlencode(data), headers={'Content-Type': 'application/x-www-form-urlencoded'})
   strContent = content.decode('utf-8')
   print(resp)
   print(strContent)


#get('categories/1')
#get('products/1')
add('categories')



category_new.xml:

<?xml version="1.0" encoding="UTF-8"?>


1
1

some name
some name
some name
some name
some name
some name

       <link_rewrite>
       </link_rewrite>
       <meta_title>
       </meta_title>
       <meta_description>
       </meta_description>
       <meta_keywords>
       </meta_keywords>






Hope it helps testing new Prestashop ;)

Link to comment
Share on other sites

Very Helpful!
In addition, if you want to UPDATE objects in the shop, you should you use HTTP PUT request to the object url and use xml file with update data as a payload to PUT request.

Here is small example in python (I love python too).
It updates product quantity to 11 for product combination with id=7:

import base64
from google.appengine.api import urlfetch

base64string = base64.encodestring(
           '%s:' % (KEY,))[:-1]
authheader =  "Basic %s" % base64string

xml = '''<?xml version="1.0" encoding="UTF-8"?>


2
11


'''

result = urlfetch.fetch(url="http://yourdomain.com/api/combinations/7",
                       payload=xml,
                       method=urlfetch.PUT,
                       headers={'Content-Type': 'application/x-www-form-urlencoded',
                                'Authorization': authheader})
return result.content



For some reason you also need to specify id_product field, it returns error without it.

Link to comment
Share on other sites

Thanks for heads up on updating, but I didn't get that far yet :D

I'm stuck on adding new product. I can add a product but I can't get it to show up in any category. I'm assuming that I'm doing something wrong with associations here, or it's a bug.

The xml I'm sending is basically the same as the one I'm receiving for correct product but without the and elements.

If anyone has any idea about how to associate product with category through webservice, I would be grateful for posting it here.

Update:
It's probably a bug: Bug report: 8535

Link to comment
Share on other sites

Thank you for your help.
When i looked at your xml I noticed that I was sending for some strange reason outside of the product /> scope.
After correcting this everything work as it should.

Did anyone figured out how to send images yet?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...