Magento无法列出客户

时间:2014-11-03 23:46:54

标签: python magento

我尝试了以下代码。其他API正在运行,只有customerCustomerList不起作用..

def main():
    client = SOAPpy.WSDL.Proxy(WSDL_ADDR)
    sid = client.login(username=USERNAME,apiKey=APIKEY)
    print 'Logined as session id %s' % sid

    print 'Listing customers...'
    res = client.customerCustomerList(sid)
    print res

    client.endSession(sessionId=sid)

它给出了以下例外:

/usr/local/lib/python2.7/site-packages/SOAPpy/Client.pyc in __call__(self, *args, **kw)
    545                     return self.__f_call(*args, **kw)
    546             else:
--> 547                 return self.__r_call(*args, **kw)
    548
    549         def __getattr__(self, name):

/usr/local/lib/python2.7/site-packages/SOAPpy/Client.pyc in __r_call(self, *args, **kw)
    567         def __r_call(self, *args, **kw):
    568             return self.__call(self.__name, args, kw, self.__ns, self.__sa,
--> 569                 self.__hd, self.__ma)
    570
    571         def __repr__(self):

/usr/local/lib/python2.7/site-packages/SOAPpy/Client.pyc in __call(self, name, args, kw, ns, sa, hd, ma)
    469
    470
--> 471         p, attrs = parseSOAPRPC(r, attrs = 1)
    472
    473         try:

/usr/local/lib/python2.7/site-packages/SOAPpy/Parser.pyc in parseSOAPRPC(xml_str, header, body, attrs, rules, ignore_ext)
   1103 def parseSOAPRPC(xml_str, header = 0, body = 0, attrs = 0, rules = None, ignore_ext=None):
   1104
-> 1105     t = _parseSOAP(xml_str, rules = rules, ignore_ext=ignore_ext)
   1106     p = t.body[0]
   1107

/usr/local/lib/python2.7/site-packages/SOAPpy/Parser.pyc in _parseSOAP(xml_str, rules, ignore_ext, forbid_entities, forbid_external, forbid_dtd)
   1086         parser._parser = None
   1087         print traceback.format_exc()
-> 1088         raise e
   1089
   1090     return t

SAXParseException: <unknown>:1:0: no element found

我尝试使用过滤器减少要返回的数据量,但它也没有用。

    res = client.customerCustomerList(sid,{
            'group_id':'1',
            'store_id':'1'
        })

customerCustomerList()在WSDL中,因此它可能不是权限问题。

我目前的解决方法是枚举customerID并逐个获取,这远非理想......

print 'Listing all the customers...'
for i in xrange(0,999999):
    try:
        res = client.customerCustomerInfo(sid,str(i))
        print res
    except Exception ,ex:
        sys.stderr.write( str(ex)+'\n')

2 个答案:

答案 0 :(得分:0)

'SAXParseException :: 1:0:找不到元素'问题意味着什么服务器返回一些答案但不是XML格式。

而不是:

res = client.customerCustomerList(sid)

试试这个:

res = client.customerCustomerList(sid, None)

或者这个:

complex_filter = [{'complex_filter': [{
    'key': 'customer_id',
    'value': [{
        'key': 'gt',
        'value': 0}]
    }]
}]
res = client.customerCustomerList(sid, complex_filter)

答案 1 :(得分:0)

无论如何,我通过应用Filters来一次获取一个记录子集来获得代码。 Magento API确实应该提供分页。

使用SOAPpy无效,无论如何,代码使用suds

from datetime import datetime,timedelta
from suds.client import Client

client = Client(MAGENTO_WSDL_ADDRESS)
sid = client.service.login(username=USER_NAME, apiKey=API_KEY)

# Fetch all new users in last 24 hours
filter = {  
    "complex_filter":{  
        "item":{  
            "key":"created_at",
            "value":{  
                "key":"from",
                "value": datetime.strftime(datetime.now() - timedelta(hours=24), "%Y-%m-%d %H:%M:%S")
            }
        }
    }
}

res = client.services.customerCustomerList(sid, filters=filter)

# New customer ids from last 24 hours.
new_uids = [x.customer_id for x in res]
相关问题