[python]代码库
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, urllib
from urllib import urlencode
#----------------------------------
# 游戏充值调用示例代码 - 聚合数据
# 在线接口文档:http://www.juhe.cn/docs/88
#----------------------------------
def main():
#配置您申请的APPKey
appkey = "*********************"
#1.商品小类列表
request1(appkey,"GET")
#2.商品信息
request2(appkey,"GET")
#3.商品价格查询
request3(appkey,"GET")
#4.游戏直充区服查询
request4(appkey,"GET")
#5.游戏直充
request5(appkey,"GET")
#6.订单状态查询
request6(appkey,"GET")
#商品小类列表
def request1(appkey, m="GET"):
url = "http://op.juhe.cn/ofpay/game/cardlist"
params = {
"key" : appkey, #应用APPKEY(应用详细页查询)
}
params = urlencode(params)
if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params))
else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content)
if res:
error_code = res["error_code"]
if error_code == 0:
#成功请求
print res["result"]
else:
print "%s:%s" % (res["error_code"],res["reason"])
else:
print "request api error"
#商品信息
def request2(appkey, m="GET"):
url = "http://op.juhe.cn/ofpay/game/cardinfo"
params = {
"cardid" : "", #对应接口1的cardid
"key" : appkey, #应用APPKEY(应用详细页查询)
}
params = urlencode(params)
if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params))
else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content)
if res:
error_code = res["error_code"]
if error_code == 0:
#成功请求
print res["result"]
else:
print "%s:%s" % (res["error_code"],res["reason"])
else:
print "request api error"
#商品价格查询
def request3(appkey, m="GET"):
url = "http://op.juhe.cn/ofpay/game/cardprice"
params = {
"cardid" : "", #对应接口2的cardid
"key" : appkey, #应用APPKEY(应用详细页查询)
}
params = urlencode(params)
if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params))
else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content)
if res:
error_code = res["error_code"]
if error_code == 0:
#成功请求
print res["result"]
else:
print "%s:%s" % (res["error_code"],res["reason"])
else:
print "request api error"
#游戏直充区服查询
def request4(appkey, m="GET"):
url = "http://op.juhe.cn/ofpay/game/areaserver"
params = {
"cardid" : "", #对应接口3的cardid
"key" : appkey, #应用APPKEY(应用详细页查询)
}
params = urlencode(params)
if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params))
else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content)
if res:
error_code = res["error_code"]
if error_code == 0:
#成功请求
print res["result"]
else:
print "%s:%s" % (res["error_code"],res["reason"])
else:
print "request api error"
#游戏直充
def request5(appkey, m="GET"):
url = "http://op.juhe.cn/ofpay/game/order"
params = {
"cardid" : "", #商品编码,对应接口3的cardid
"cardnum" : "", #购买数量
"orderid" : "", #订单号,8-32位数字字母组合
"game_userid" : "", #游戏玩家账号(game_userid=xxx@162.com$xxx001 xxx@162.com是通行证xxx001是玩家账号)
"game_area" : "", #游戏所在区域,没有则不填,具体参照接口4返回,URLEncode UTF8
"game_srv" : "", #游戏所在服务器,没有则不填,具体参照接口4返回,URLEncode UTF8
"key" : appkey, #应用APPKEY(应用详细页查询)
"sign" : "", #校验值,md5(<b>OpenID</b>+key+cardid+cardnum+orderid+game_userid+game_area+game_srv)
}
params = urlencode(params)
if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params))
else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content)
if res:
error_code = res["error_code"]
if error_code == 0:
#成功请求
print res["result"]
else:
print "%s:%s" % (res["error_code"],res["reason"])
else:
print "request api error"
#订单状态查询
def request6(appkey, m="GET"):
url = "http://op.juhe.cn/ofpay/game/ordersta"
params = {
"orderid" : "", #商家订单号,8-32位字母数字组合
"key" : appkey, #应用APPKEY(应用详细页查询)
}
params = urlencode(params)
if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params))
else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content)
if res:
error_code = res["error_code"]
if error_code == 0:
#成功请求
print res["result"]
else:
print "%s:%s" % (res["error_code"],res["reason"])
else:
print "request api error"
if __name__ == '__main__':
main()