123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- //require_once dirname(__FILE__).'/DB_PDO.class.php';
- //require_once 'TBK.class.php';
- //require_once dirname(__FILE__).'/conf.class.php';
- /**
- * 更新优选商品剩余数量数据
- */
- #设置时区
- date_default_timezone_set('PRC');
- ini_set('memory_limit','1024M');
- //$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";
- $url = "https://api.data.caasdata.com/channels/69199/ta";
- $result = curl_get($url);
- $sql = format_sql($result['data'], 'ks_user_info');
- echo $sql;exit;
- echo '<pre>';
- print_r($result);exit;
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $total_num = 0;
- $ins_num = 0;
- $already = 0;
- foreach($result['data']['data'] as $k=>$v){
- $total_num++;
- if(!if_exists($ks_id)){
- $sql = format_sql($v, 'ks_users');
- }else{
- $already++;
- continue;
- }
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $_PDO->lastinsertid();
- if($res){
- $ins_num++;
- }
- }
- echo "\n总共".$total_num."条数据";
- echo "\n成功插入".$ins_num."条数据";
- echo "\n已存在".$already."条数据";
- function curl_get($url){
-
- $header = array(
- 'Origin: https://www.caasdata.com',
- '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',
- 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwaS5kYXRhLmNhYXNkYXRhLmNvbS91c2VycyIsImlhdCI6MTU1NjQzNjU2NywiZXhwIjoxNTU5MDI4NTY3LCJuYmYiOjE1NTY0MzY1NjcsImp0aSI6Ik45ekZrRFBadEVwT2pKSFgiLCJzdWIiOiI2OTY2NyIsIkxvZ2luVG9rZW4iOiJJakZlejFUV3lCRkJ3In0.DkA2GnIZKm-dSoi0mHGOnZ-F2-0d1XuS3E6Gr2HGXNw',
- );
- $ch = curl_init();//初始化curl
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HEADER, 0);//
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- //curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
- //curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- return json_decode($data,true);
- }
- function curl_post($url, $param){
- $auth_token = isset($param['auth_token'])? $param['auth_token'] : '';
- unset($param['auth_token']);
- $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token);
- $ch = curl_init();//初始化curl
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HEADER, 0);//
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- return json_decode($data,true);
- }
- function if_exists($ks_id){
- $sql = "select id from ks_users where ks_id = {$ks_id} limit 1";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- if($res){
- return true;
- }
- return false;
- }
- function format_sql($data, $table){
- if(empty($data)) return false;
- $columns = '';
- $values = '';
- foreach($data as $key=>$val){
- if($key == 'id'){
- $key = 'ks_id';
- }
- $columns .= '`'.$key.'`,';
- if(is_array($val)){
- $val = json_encode($val);
- }
- $values .= "'".$val."',";
- }
- $columns = trim($columns, ',');
- $values = trim($values,',');
- $sql = 'insert into '.$table.' ('.$columns.') values('.$values.')';
- return $sql;
- }
|