优惠券订单及其他脚本

web_red_data.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. require_once dirname(dirname(__FILE__)).'/DB_PDO.class.php';
  3. //require_once 'TBK.class.php';
  4. require_once dirname(dirname(__FILE__)).'/conf.class.php';
  5. /**
  6. * 更新优选商品剩余数量数据
  7. */
  8. #设置时区
  9. date_default_timezone_set('PRC');
  10. ini_set('memory_limit','1024M');
  11. $plat_arr = array(
  12. 0 => 998, //抖音
  13. 1 => 993, //快手
  14. 2 => 58, //bilibili
  15. 3 => 94, //美拍
  16. 4 => 95, //秒拍
  17. 5 => 997, //西瓜视频
  18. 6 => 996, //火山小视频
  19. );
  20. $tag_arr = array(
  21. 0 => 0, //全部
  22. 1 => 30, //搞笑
  23. 2 => 29, //段子
  24. 3 => 73, //时尚
  25. 4 => 34, //美食
  26. 5 => 77, //旅游
  27. 6 => 40, //游戏
  28. 7 => 45, //娱乐
  29. 8 => 33, //音乐
  30. 9 => 50, //创意
  31. 10 => 41, //健身
  32. 11 => 308, //手工手绘
  33. 12 => 49, //萌娃
  34. 13 => 38, //宠物
  35. 14 => 35, //美妆
  36. 15 => 32, //舞蹈
  37. 16 => 44, //鬼畜
  38. 17 => 46, //影视
  39. 18 => 63, //科技
  40. 19 => 47, //小姐姐
  41. 20 => 48, //小哥哥
  42. 21 => 53, //民间艺人
  43. 22 => 78, //母婴
  44. 23 => 74, //汽车
  45. 24 => 31, //高颜值
  46. );
  47. $timer_arr = array(0,1,2,3);// 最多同步最近4期榜单
  48. /**
  49. * 同步
  50. *
  51. */
  52. $total = 0;
  53. $success = 0;
  54. foreach($plat_arr as $platform){
  55. foreach($tag_arr as $tag){
  56. foreach($timer_arr as $timer){
  57. $total++;
  58. $res = sync_data($platform, $tag, $timer, $total);
  59. if($res){
  60. $success++;
  61. }
  62. }
  63. }
  64. }
  65. echo "\n总共同步{$total}次,成功同步{$success}次";
  66. function sync_data($platform, $tag, $timer, $total){
  67. echo "\n\n/************* Start-{$total} **************/";
  68. echo "\nplatform:".$platform." tag:".$tag." timer:".$timer;
  69. $url = "https://api.data.caasdata.com/rank/lists?time=1556439520000&dataType=kol&tag={$tag}&timer={$timer}&page=1&timeType=week&platform={$platform}&count=50&data_counter=5";
  70. $result = curl_get($url);
  71. if(empty($result['data']['data'])){
  72. echo "\n没有数据!";
  73. echo "\n".'/*** End ***/';
  74. return false;
  75. }
  76. #第二页
  77. $url = "https://api.data.caasdata.com/rank/lists?time=1556439520000&dataType=kol&tag={$tag}&timer={$timer}&page=2&timeType=week&platform={$platform}&count=50&data_counter=5";
  78. $result2 = curl_get($url);
  79. if(empty($result2['data']['data'])){
  80. $_data = $result['data']['data'];
  81. }else{
  82. $_data = array_merge($result['data']['data'], $result2['data']['data']);
  83. }
  84. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  85. $total_num = 0;
  86. $ins_num = 0;
  87. $already = 0;
  88. foreach($_data as $k=>$v){
  89. $total_num++;
  90. if(!if_exists($v['id'])){
  91. $sql = format_sql($v, 'ks_users');
  92. }else{
  93. $already++;
  94. continue;
  95. }
  96. $_PDO->beginTransaction();
  97. try {
  98. $stmt = $_PDO->prepare($sql);
  99. $stmt->execute();
  100. $res = $_PDO->lastinsertid();
  101. if($res){
  102. #插入详情
  103. $ins_info = ins_info($v['id']);
  104. $ins_num++;
  105. $_PDO->commit();
  106. }else{
  107. $_PDO->rollback();
  108. return false;
  109. }
  110. } catch(PDOException $e) {
  111. $_PDO->rollback();
  112. echo "\n异常:".$e->getMessage();
  113. return false;
  114. }
  115. }
  116. echo "\n总共".$total_num."条数据";
  117. echo "\n成功插入".$ins_num."条数据";
  118. echo "\n已存在".$already."条数据";
  119. echo "\n".'/*** End ***/';
  120. return true;
  121. }
  122. function curl_get($url){
  123. $header = array(
  124. 'Origin: https://www.caasdata.com',
  125. 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
  126. 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwaS5kYXRhLmNhYXNkYXRhLmNvbS91c2VycyIsImlhdCI6MTU1NjQzNjU2NywiZXhwIjoxNTU5MDI4NTY3LCJuYmYiOjE1NTY0MzY1NjcsImp0aSI6Ik45ekZrRFBadEVwT2pKSFgiLCJzdWIiOiI2OTY2NyIsIkxvZ2luVG9rZW4iOiJJakZlejFUV3lCRkJ3In0.DkA2GnIZKm-dSoi0mHGOnZ-F2-0d1XuS3E6Gr2HGXNw',
  127. );
  128. $ch = curl_init();//初始化curl
  129. curl_setopt($ch, CURLOPT_URL,$url);
  130. curl_setopt($ch, CURLOPT_HEADER, 0);//
  131. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  132. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
  134. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  135. //curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  136. //curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  137. $data = curl_exec($ch);//运行curl
  138. curl_close($ch);
  139. return json_decode($data,true);
  140. }
  141. function curl_post($url, $param){
  142. $auth_token = isset($param['auth_token'])? $param['auth_token'] : '';
  143. unset($param['auth_token']);
  144. $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token);
  145. $ch = curl_init();//初始化curl
  146. curl_setopt($ch, CURLOPT_URL,$url);
  147. curl_setopt($ch, CURLOPT_HEADER, 0);//
  148. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  149. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  150. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
  151. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  152. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  153. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  154. $data = curl_exec($ch);//运行curl
  155. curl_close($ch);
  156. return json_decode($data,true);
  157. }
  158. function if_exists($ks_id){
  159. $sql = "select id from ks_users where ks_id = {$ks_id} limit 1";
  160. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  161. $stmt = $_PDO->prepare($sql);
  162. $stmt->execute();
  163. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  164. if($res){
  165. return true;
  166. }
  167. return false;
  168. }
  169. function format_sql($data, $table){
  170. if(empty($data)) return false;
  171. $columns = '';
  172. $values = '';
  173. foreach($data as $key=>$val){
  174. if($key == 'id'){
  175. $key = 'ks_id';
  176. }
  177. $columns .= '`'.$key.'`,';
  178. if(is_array($val)){
  179. $val = json_encode($val);
  180. }
  181. $values .= "'".$val."',";
  182. }
  183. $columns = trim($columns, ',');
  184. $values = trim($values,',');
  185. $sql = 'insert into '.$table.' ('.$columns.') values('.$values.')';
  186. return $sql;
  187. }
  188. function ins_info($ks_id){
  189. $url = "https://api.data.caasdata.com/channels/{$ks_id}/ta";
  190. $result = curl_get($url);
  191. //echo '<pre>';
  192. //print_r($result);exit;
  193. if(empty($result['data'])) return false;
  194. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  195. $sql = format_sql($result['data'], 'ks_user_info');
  196. $stmt = $_PDO->prepare($sql);
  197. $stmt->execute();
  198. $res = $_PDO->lastinsertid();
  199. return $res;
  200. }