淘宝API的使用例子

http://hi.baidu.com/kalada/item/b1b498279028921409750866

建议PHP开发者使用JSON,因为PHP5对JSON有很好的支持。
以下是对taobao.shopcats.list.get接口调用实例。
API官方文档在这里:
http://isv.alisoft.com/isv/html/showhtml.jspa?html=/html/taobao/api/taobao.shopcats.list.get.html

/**
* 淘宝API调试
*/

function getCats(){
$tvs = new SecurityEnhancedWebService();
$res = $tvs->taobao_getCates();
print_r(json_decode($res));
}



public function taobao_getCates($parent_cid = '0', $v = '1.0', $sip_format = 'json'){
$proxy = new Snoopy();
//准备本次请求参数
$timestamp = date("Y-m-d H:i:s");//'2008-05-02 21:18:58';
$sip_sessionid = '294fe843d98a455da8918f472a9c3c93';
$formVars['sip_timestamp'] = $timestamp;
$formVars['sip_appkey'] = $this->APP_ID;
$formVars['sip_apiname'] = 'taobao.shopcats.list.get';
$formVars['v'] = $v;
$formVars['parent_cid'] = $parent_cid;
$formVars['sip_sessionid'] = $sip_sessionid;
$formVars['format'] = $sip_format;
ksort($formVars);
// print_r($formVars);
//生成签名
$signStr = $this->CERT_CODE;
foreach ($formVars as $key => $val) {
$signStr.= $key;
$signStr.= $val;
}
$sip_sign = strtoupper(md5($signStr));
$formVars['sip_sign'] = $sip_sign;
//向AEP发出服务端请求
$proxy->submit($this->SIP_URL_PRE,$formVars);

return ($proxy->results);
}
.NET调用validateUser接口举例(代码) 这里对.NET如何调用互联平台接口通过举例做下介绍。
首先,在我的工作台-》注册软件,获得APP_ID和CERT_CODE,在管理我的软件里,录入软件测试URL,我这里举例http://localhost:2715/RestTest/Default.aspx。
然后,本地系统里编写接口调用代码,下面是我的举例系统内Default.aspx.cs调用代码
protected void Page_Load(object sender, EventArgs e)
{
HttpRequest request = HttpContext.Current.Request;
//在软件应用中心跳转到ISV应用时的URL参数中取得,user_id、app_instance_id和token
string aepUserId = request.Params["user_id"];
string aepInstanceId = request.Params["app_instance_id"];
string appId = request.Params["app_id"]; //软件注册时获得
string token = request.Params["token"]; //每次点击生成不一样的token,并只有10秒钟有效
string code = "6c0a91f01a5411dd8179bad18f04aace";//软件注册时获得
System.DateTime timestamp = System.DateTime.Now;//时间获得当前系统时间

//sip_sign签名字段的生成,将CERT CODE和所有接口要传的参数进行组合,再将组装好的字符串进行md5加密后转成16进制后得到一个32位的密文
string sipsign = code + "appId" + appId + "appInstanceId" + aepInstanceId + "sip_apinamealisoft.validateUser" + "sip_appkey" + appId + "sip_timestamp" + timestamp + "token" + token + "userId" + aepUserId;

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
sipsign =BitConverter.ToString( md5.ComputeHash(Encoding.UTF8.GetBytes(sipsign))).Replace("-","");
//sip_sign生成END

//系统级参数拼装,包括sip_appkey、sip_apiname、sip_timestamp和sip_sign,sip_apiname是接口名这里举validateUser为例
string SIPdate = "sip_appkey=" + appId + "&sip_apiname=alisoft.validateUser&sip_timestamp="+timestamp+"&sip_sign="+sipsign;
//接口级参数拼装
string apidate="&userId="+aepUserId+"&appId="+appId+"&appInstanceId="+aepInstanceId+"&token="+token;

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postdata =encoding.GetBytes(SIPdate + apidate);//所有要传参数拼装
// Prepare web request...
//目前阿里软件的服务集成平台(SIP)的接口测试地址是:http://sipdev.alisoft.com/sip/rest,生产环境地址是:http://sip.alisoft.com/sip/rest,
//这里使用测试接口先,到正式上线时需要做切换
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://sipdev.alisoft.com/sip/rest");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postdata.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(postdata, 0, postdata.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();

//解析接口返回值,这里选用XML格式的解析,接口默认返回是XML格式
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(content);
XmlNode xn = xmlDoc.SelectSingleNode("String");
XmlElement xe = (XmlElement)xn;
string result = xe.InnerText;
if ("1".Equals(result))
{
//如果是使用者。。。。
windowalert("使用者");
}
else if ("0".Equals(result))
{
//如果是订阅者。。。。
windowalert("订阅者");
}
}

private void windowalert(string str)
{
HttpResponse Response = HttpContext.Current.Response;
Response.Write("<script>alert('" + str + "');window.close(true);</script>");
}

}
最后,启本地应用服务,在管理我的软件里点“测试软件”按钮,就可以登录到自己的应用服务,并且成功调用了互联平台上的验证用户接口哦。
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(4306) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~
评论热议
posted @ 2016-02-01 00:00  张同光  阅读(124)  评论(0编辑  收藏  举报