改了一下淘宝api demo..

编程技术  /  houtizong 发布于 3年前   99
改了一下淘宝api domo
改前:
#此DEMO的功能:查询手机类的淘客商品require 'digest/md5'require 'net/http'require 'open-uri'require 'iconv'#获得当前时间t = Time.new();#组装参数paramArray = {#组装协议参数  'app_key'=>'test',  'method'=>'taobao.taobaoke.items.get',  'format'=>'xml',  'v'=>'1.0',  'timestamp'=>t.strftime("%Y-%m-%d %H:%M:%S"),  #组装应用参数  'fields'=>'iid,title,nick,pic_url,price,click_url',  'pid' => 'mm_5410_0_0',  'cid' => '1512',  'page_no' => '1',  'page_size' => '6'}#生成签名def sign(param,sercetCode)array = param.sort()  i = 0  str = sercetCode  while i < param.length()    temp = array[i]      str = str + temp[0] + temp[1]      i = i + 1      end  str = Digest::MD5.hexdigest(str)  return str.upcase()end#组装请求参数def createRequestParam(paramArray)array = paramArray.sort()  i = 0  str = ''  while i < paramArray.length()    temp = array[i]    str = str + temp[0] + '=' + temp[1] + '&'    i = i + 1      end  return strend#URL encodedef URLEncode(str)return str.gsub!(/[^\w$&\-+.,\/:;=?@]/) { |x| x = format("%%%x", x[0])}  end#把str的编码转化为GBK编码def to_gbk(str)Iconv.iconv("GBK//IGNORE","UTF-8//IGNORE",str).to_sendurl = 'http://gw.sandbox.taobao.com/router/rest?'url = url + createRequestParam(paramArray)+'sign=' + sign(paramArray,'test')parsedURL = URI.parse(URLEncode(url))#请求生成的URL,把结果输出Net::HTTP.version_1_2open(parsedURL)do|http|    puts to_gbk(http.read)end

My code:
require'md5'require'cgi'require'open-uri'params = {               'app_key'    => 'test',               'method'      => 'taobao.taobaoke.items.get',               'format'      => 'xml',               'v'              => '1.0',               'timestamp' => Time.now.strftime("%Y-%m-%d %H:%M:%S"),               'fields'      => 'iid,title,nick,pic_url,price,click_url',               'pid'           => 'mm_5410_0_0',               'cid'           => '1512',               'page_no'    => '1',               'page_size' => '6'}def sign(params,secret_code)  Digest::MD5.hexdigest(params.sort.flatten.unshift(secret_code).join).upcaseenddef create_request_params params  params.map{|key,value| "#{key}=#{CGI.escape(value)}"}.join("&")endurl = 'http://gw.sandbox.taobao.com/router/rest?'url = url + create_request_params(params.merge('sign' =>sign(params,'test')))open(url) do |resp|  p resp.readend

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客