优惠券订单及其他脚本

web_red_detail.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. //require_once dirname(__FILE__).'/DB_PDO.class.php';
  3. //require_once 'TBK.class.php';
  4. //require_once dirname(__FILE__).'/conf.class.php';
  5. /**
  6. * 更新优选商品剩余数量数据
  7. */
  8. #设置时区
  9. date_default_timezone_set('PRC');
  10. ini_set('memory_limit','1024M');
  11. //$url = "https://api.data.caasdata.com/rank/lists?time=1556439520000&dataType=kol&tag=0&timer=0&page=1&timeType=week&platform=993&count=50&data_counter=5";
  12. $url = "https://api.data.caasdata.com/channels/69199/ta";
  13. $result = curl_get($url);
  14. $sql = format_sql($result['data'], 'ks_user_info');
  15. echo $sql;exit;
  16. echo '<pre>';
  17. print_r($result);exit;
  18. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  19. $total_num = 0;
  20. $ins_num = 0;
  21. $already = 0;
  22. foreach($result['data']['data'] as $k=>$v){
  23. $total_num++;
  24. if(!if_exists($ks_id)){
  25. $sql = format_sql($v, 'ks_users');
  26. }else{
  27. $already++;
  28. continue;
  29. }
  30. $stmt = $_PDO->prepare($sql);
  31. $stmt->execute();
  32. $res = $_PDO->lastinsertid();
  33. if($res){
  34. $ins_num++;
  35. }
  36. }
  37. echo "\n总共".$total_num."条数据";
  38. echo "\n成功插入".$ins_num."条数据";
  39. echo "\n已存在".$already."条数据";
  40. function curl_get($url){
  41. $header = array(
  42. 'Origin: https://www.caasdata.com',
  43. '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',
  44. 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwaS5kYXRhLmNhYXNkYXRhLmNvbS91c2VycyIsImlhdCI6MTU1NjQzNjU2NywiZXhwIjoxNTU5MDI4NTY3LCJuYmYiOjE1NTY0MzY1NjcsImp0aSI6Ik45ekZrRFBadEVwT2pKSFgiLCJzdWIiOiI2OTY2NyIsIkxvZ2luVG9rZW4iOiJJakZlejFUV3lCRkJ3In0.DkA2GnIZKm-dSoi0mHGOnZ-F2-0d1XuS3E6Gr2HGXNw',
  45. );
  46. $ch = curl_init();//初始化curl
  47. curl_setopt($ch, CURLOPT_URL,$url);
  48. curl_setopt($ch, CURLOPT_HEADER, 0);//
  49. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  50. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  51. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
  52. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  53. //curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  54. //curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  55. $data = curl_exec($ch);//运行curl
  56. curl_close($ch);
  57. return json_decode($data,true);
  58. }
  59. function curl_post($url, $param){
  60. $auth_token = isset($param['auth_token'])? $param['auth_token'] : '';
  61. unset($param['auth_token']);
  62. $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token);
  63. $ch = curl_init();//初始化curl
  64. curl_setopt($ch, CURLOPT_URL,$url);
  65. curl_setopt($ch, CURLOPT_HEADER, 0);//
  66. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  67. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  68. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
  69. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  70. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  71. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  72. $data = curl_exec($ch);//运行curl
  73. curl_close($ch);
  74. return json_decode($data,true);
  75. }
  76. function if_exists($ks_id){
  77. $sql = "select id from ks_users where ks_id = {$ks_id} limit 1";
  78. $stmt = $_PDO->prepare($sql);
  79. $stmt->execute();
  80. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  81. if($res){
  82. return true;
  83. }
  84. return false;
  85. }
  86. function format_sql($data, $table){
  87. if(empty($data)) return false;
  88. $columns = '';
  89. $values = '';
  90. foreach($data as $key=>$val){
  91. if($key == 'id'){
  92. $key = 'ks_id';
  93. }
  94. $columns .= '`'.$key.'`,';
  95. if(is_array($val)){
  96. $val = json_encode($val);
  97. }
  98. $values .= "'".$val."',";
  99. }
  100. $columns = trim($columns, ',');
  101. $values = trim($values,',');
  102. $sql = 'insert into '.$table.' ('.$columns.') values('.$values.')';
  103. return $sql;
  104. }