淘客api返回的值是utf8,如何转换成GBK
我的站是GBK编码,运行了好长时间了,百度收录,排名流量都不错
现在想在我首页挂上一行淘宝的产品,想用api来调用,参照网上的程序已经弄出来了,但是遇到一个非常麻烦的问题。
我站的编码是GBK,但是淘宝api返回的值是xml是utf8编码
不知道有没有这样的函数可以把这段代码转换成gb2312编码。
淘宝api返回值是这样的内容:
XML code
<?xml version="1.0" encoding="utf-8" ?>
<taobaoke_items_get_response>
<taobaoke_items list="true">
<taobaoke_item><commission_num>82</commission_num><nick>绝美天使</nick><num_iid>6806006478</num_iid><pic_url>http://img04.taobaocdn.com/bao/uploaded/i4/T1NIVHXkJgXXc1Nl_b_094851.jpg</pic_url><price>126.00</price><title>绝美天使细节 2011春装荷叶边宫廷华贵亚光色丁长袖蕾丝衬衫6502</title></taobaoke_item>
......
<taobaoke_item><commission_num>48</commission_num><nick>天使真爱77</nick><num_iid>7526905560</num_iid><pic_url>http://img04.taobaocdn.com/bao/uploaded/i4/T1vHJMXlFOXXagOjw4_053445.jpg</pic_url><price>99.00</price><title>原创全网首发金牌秒杀精梳针织细节一级雪纺衬衫打底衫 568</title></taobaoke_item>
</taobaoke_items>
<total_results>261952</total_results>
</taobaoke_items_get_response>
还请高手指教。
------解决方案--------------------
function GB2312UTF8() {
this.Dig2Dec = function(s) {
var retV = 0;
if (s.length == 4) {
for (var i = 0; i < 4; i++) {
retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
}
return retV;
}
return - 1;
}
this.Hex2Utf8 = function(s) {
var retS = "";
var tempS = "";
var ss = "";
if (s.length == 16) {
tempS = "1110" + s.substring(0, 4);
tempS += "10" + s.substring(4, 10);
tempS += "10" + s.substring(10, 16);
var sss = "0123456789ABCDEF";
for (var i = 0; i < 3; i++) {
retS += "%";
ss = tempS.substring(i * 8, (eval(i) + 1) * 8);
retS += sss.charAt(this.Dig2Dec(ss.substring(0, 4)));
retS += sss.charAt(this.Dig2Dec(ss.substring(4, 8)));
}
return retS;
}
return "";
}
this.Dec2Dig = function(n1) {
var s = "";
var n2 = 0;
for (var i = 0; i < 4; i++) {
n2 = Math.pow(2, 3 - i);
if (n1 >= n2) {
s += '1';
n1 = n1 - n2;
} else s += '0';
}
return s;
}
this.Str2Hex = function(s) {
var c = "";
var n;
var ss = "0123456789ABCDEF";
var digS = "";
for (var i = 0; i < s.length; i++) {
c = s.charAt(i);
n = ss.indexOf(c);
digS += this.Dec2Dig(eval(n));
}
return digS;
}
this.Gb2312ToUtf8 = function(s1) {
var s = escape(s1);
var sa = s.split("%");
var retV = "";
if (sa[0] != "") {
retV = sa[0];
}
for (var i = 1; i < sa.length; i++) {
if (sa[i].substring(0, 1) == "u") {
retV += this.Hex2Utf8(this.Str2Hex(sa[i].substring(1, 5)));
if (sa[i].length) {
retV += sa[i].substring(5);
}
} else {
retV += unescape("%" + sa[i]);
if (sa[i].length) {
retV += sa[i].substring(5);
}
}
}
return retV;
}
this.Utf8ToGb2312 = function(str1) {
var substr = "";
var a = "";
var b = "";
var c = "";
var i = -1;
i = str1.indexOf("%");
if (i == -1) {
return str1;