淘宝开放平台API

爱被打了一巴掌 2022-02-19 06:37 527阅读 0赞
  1. 前两天按照淘宝API提供的demo代码改写成了类

由于公司的店铺比较多,而且淘宝做了授权改造,所有API都需要使用session

所以把一些关键参数存储在了数据库之中便于调用








































































































字段 类型 属性 Null 缺省值 额外 执行操作
ID  int(10)   是  NULL  auto_increment  改变 丢弃 键名 索引 唯一
shop_nick  varchar(32)   是  NULL    改变 丢弃 键名 索引 唯一
userid  int(5)   是  NULL    改变 丢弃 键名 索引 唯一
appkey  int(10)   是  NULL    改变 丢弃 键名 索引 唯一
appsecret  varchar(50)   是  NULL    改变 丢弃 键名 索引 唯一
Session  varchar(64)   是  NULL    改变 丢弃 键名 索引 唯一
LastUp  int(10)   是  NULL    改变 丢弃 键名 索引 唯一

调用代码:

  1. <?php
  2. $tb = new TBao(1);
  3. $method='taobao.items.onsale.get';
  4. $paramArr = array(
  5. /* API应用级输入参数 Start*/
  6. 'fields' => 'num_iid,title,price,volume,list_time,num ,modified ,outer_id',
  7. 'page_no' => '1',
  8. 'page_size' => '40'
  9. /* API应用级输入参数 End*/
  10. );
  11. $result_item = $tb->get_info($method,$paramArr);
  12. ?>

接口类代码:

  1. <?php
  2. class TBao
  3. {
  4. //一些基础配置项
  5. var $url = "http://gw.api.taobao.com/router/rest?";
  6. var $format = "xml";
  7. var $v = "2.0";
  8. var $sign_method = "md5";
  9. //关键参数
  10. private $AppKey;
  11. private $AppSecret;
  12. private $top_Session;
  13. var $UserID;
  14. var $ShopNick;
  15. var $method;
  16. function __construct($TB_id=0){
  17. $this->TBao($TB_id);
  18. }
  19. /**
  20. * 根据店铺ID 构造该店铺的一些关键参数
  21. */
  22. function TBao($TB_id=0){
  23. global $DB;
  24. $ss_sql = "select * from TB_session where ID='$TB_id' ";
  25. $DB->query($ss_sql);
  26. $DB->next_record();
  27. $this->AppKey= $DB->f("appkey");
  28. $this->AppSecret= $DB->f("appsecret");
  29. $this->top_Session= $DB->f("Session");
  30. $this->UserID= $DB->f("userid");
  31. $this->ShopNick= $DB->f("shop_nick");
  32. }
  33. /**
  34. * 获取淘宝信息
  35. */
  36. function get_info($method,$paramArr=array()){
  37. $this->method = $method;
  38. //组合参数 加入一些简单的数据验证
  39. $unix_time = time();
  40. if(date("T") == "UTC"){
  41. $unix_time += 28800;
  42. }
  43. $tid = isset($paramArr['tid'])?$paramArr['tid']:0;
  44. $paramArr['timestamp'] = date("Y-m-d H:i:s",$unix_time);
  45. $paramArr['format'] = $this->format;
  46. $paramArr['v'] = $this->v;
  47. $paramArr['sign_method'] = $this->sign_method;
  48. $paramArr['method'] = $method;
  49. $paramArr['app_key'] = $this->AppKey;
  50. if(!empty($this->top_Session)){
  51. $paramArr['session'] = $this->top_Session;
  52. }
  53. //生成签名
  54. $sign = $this->createSign($paramArr);
  55. //组织参数
  56. $strParam = $this->createStrParam($paramArr);
  57. $strParam .= 'sign='.$sign;
  58. //构造Url
  59. $urls = $this->url.$strParam;
  60. //连接超时自动重试
  61. $cnt=0;
  62. while($cnt < 3 && ($tb_result=@$this->vita_get_url_content($urls))===FALSE) $cnt++;
  63. //解析数据
  64. if($this->format == "xml"){
  65. $result = $this->getXmlData($tb_result);
  66. }else{
  67. $result = json_decode($tb_result,true);
  68. }
  69. $result = $this->analyse_result($result,$tid);
  70. return $result;
  71. }
  72. /**
  73. * 处理返回结果 如果有错误 怎么记录 及 处理
  74. */
  75. function analyse_result($data,$tid=0){
  76. //var_dump($data);
  77. if(isset($data['error_response'])){
  78. $error = $data['error_response'];
  79. }elseif(isset($data['code'])){
  80. $error = $data;
  81. }else{
  82. return $data;
  83. }
  84. //怎么处理这个错误结果 输出 记录 与反馈
  85. $error_msg = $error['msg'];
  86. $error_msg .= isset($error['sub_msg'])?$error['sub_msg']:"";
  87. $error_code = $error['code'];
  88. $error_code .= isset($error['sub_code'])?$error['sub_code']:"";
  89. $error = "错误码:".$error_code.";错误信息:".$error_msg;
  90. $this->tb_log(2,$tid,$error,0);
  91. $this->_halt($error."<br>");
  92. }
  93. //处理接口调用日志
  94. function tb_log(){
  95. }
  96. //获取session授权信息
  97. function get_sessionkey(){
  98. }
  99. //获取数据兼容file_get_contents与curl
  100. function vita_get_url_content($url) {
  101. if(function_exists('file_get_contents')) {
  102. $file_contents = file_get_contents($url);
  103. } else {
  104. $ch = curl_init();
  105. $timeout = 5;
  106. curl_setopt ($ch, CURLOPT_URL, $url);
  107. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  108. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  109. $file_contents = curl_exec($ch);
  110. curl_close($ch);
  111. }
  112. return $file_contents;
  113. }
  114. //签名函数
  115. function createSign ($paramArr) {
  116. $sign = $this->AppSecret;
  117. ksort($paramArr);
  118. foreach ($paramArr as $key => $val) {
  119. if ($key !='' && $val !='') {
  120. $sign .= $key.$val;
  121. }
  122. }
  123. $sign = strtoupper(md5($sign.$this->AppSecret));
  124. return $sign;
  125. }
  126. //组参函数
  127. function createStrParam ($paramArr) {
  128. $strParam = '';
  129. foreach ($paramArr as $key => $val) {
  130. if ($key != '' && $val !='') {
  131. $strParam .= $key.'='.urlencode($val).'&';
  132. }
  133. }
  134. return $strParam;
  135. }
  136. //解析xml函数
  137. function getXmlData ($strXml) {
  138. $pos = strpos($strXml, 'xml');
  139. if ($pos) {
  140. $xmlCode=simplexml_load_string($strXml,'SimpleXMLElement');
  141. $arrayCode=$this->get_object_vars_final($xmlCode);
  142. return $arrayCode ;
  143. } else {
  144. return '';
  145. }
  146. }
  147. function get_object_vars_final($obj){
  148. if(is_object($obj)){
  149. $obj=get_object_vars($obj);
  150. }
  151. if(is_array($obj)){
  152. foreach ($obj as $key=>$value){
  153. $obj[$key]=$this->get_object_vars_final($value);
  154. }
  155. }
  156. return $obj;
  157. }
  158. //停止程序输出错误信息
  159. function _halt($msg=''){
  160. exit($msg);
  161. }
  162. }
  163. ?>

淘宝session获取的程序

  1. <?PHP
  2. header("Content-type: text/html; charset=utf-8");
  3. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
  4. header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
  5. header("Cache-Control: no-cache, must-revalidate" );
  6. header("Pragma: no-cache" );
  7. $top_appkey = $_GET['top_appkey'];
  8. $top_parameters = $_GET['top_parameters'];
  9. $top_session = $_GET['top_session'];
  10. $top_sign = $_GET['top_sign'];
  11. if(empty($top_appkey)){
  12. $uid = $_GET['uid'];
  13. $uid_to_appkey = array('1'=>'123456');//123456为淘宝分配的AppKey
  14. die(header("Location:http://container.api.taobao.com/container?appkey=".$uid_to_appkey[$uid]."&encode=utf-8"));
  15. }
  16. $appkey_to_appsecret = array( '123456'=>'xxxxx');//xxxxx为淘宝分配的AppSecret
  17. $app_secret = $appkey_to_appsecret[$top_appkey];
  18. $unix_time = time();
  19. if(date("T") == "UTC"){
  20. $unix_time += 28800;
  21. }
  22. //验证sign 是否一致 规则:base64(md5(top_appkey+top_parameters+top_session+app_secret))
  23. $check_sign = base64_encode(md5($top_appkey.$top_parameters.$top_session.$app_secret,true));
  24. if($top_sign != $check_sign){
  25. exit("非法访问");
  26. }
  27. //解析top_parameters 获取上下文参数
  28. $parameters = hashmap(base64_decode($top_parameters));
  29. echo "<pre>";
  30. print_r($parameters);
  31. //验证时间是否在5分钟之内(前后一共10分钟)
  32. $check_time = round($parameters['ts']/1000);
  33. if($check_time < $unix_time-300 || $check_time > $unix_time+300){
  34. //exit("时间不合法");
  35. }
  36. //将top_session存储至文件中便于使用 改为存入数据库!!!!
  37. $sql = "update TB_session set Session ='$top_session' , LastUp = unix_timestamp() where appkey='$top_appkey'";
  38. //echo $sql;
  39. $DB->query($sql);
  40. echo "失效时长:".$parameters['expires_in']."<br>";
  41. echo $top_session."完毕";
  42. //转化parameter的方法
  43. function hashmap($data){
  44. $result = array();
  45. $t1 = explode("&",$data);
  46. if(!empty($t1) && isset($t1[0])){
  47. foreach($t1 as $item){
  48. $t2 = explode("=",$item);
  49. if(!empty($t2) && isset($t2[0])){
  50. $result[$t2[0]] = isset($t2[1])?$t2[1]:"";
  51. }
  52. }
  53. }
  54. return $result;
  55. }
  56. ?>

发表评论

表情:
评论列表 (有 0 条评论,527人围观)

还没有评论,来说两句吧...

相关阅读

    相关 开放API

    前两天按照淘宝API提供的demo代码改写成了类 由于公司的店铺比较多,而且淘宝做了授权改造,所有API都需要使用session 所以把一些关键参数存储在了数据库之