stanley-king 9 năm trước cách đây
mục cha
commit
1021b9bf37
2 tập tin đã thay đổi với 191 bổ sung0 xóa
  1. 67 0
      core/framework/function/http.php
  2. 124 0
      crontab/fetch_goods.php

+ 67 - 0
core/framework/function/http.php

@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * http请求函数
+ *
+ * 
+ *
+ */
+
+defined('InShopNC') or exit('Access Invalid!');
+
+function http_request($url, $params = array(), $method = 'GET', $multi = false, $extheaders = array()) 
+{
+    if (!function_exists('curl_init')) {  
+        return NULL;
+    }
+
+    $method = strtoupper($method);
+    $ci = curl_init();
+    curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
+    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
+    curl_setopt($ci, CURLOPT_TIMEOUT, 3);
+    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
+    curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
+    curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
+    curl_setopt($ci, CURLOPT_HEADER, false);
+    $headers = (array) $extheaders;
+    switch ($method) 
+    {
+        case 'POST':
+            curl_setopt($ci, CURLOPT_POST, TRUE);
+            if (!empty($params)) 
+            {
+                if ($multi) 
+                {
+                    foreach ($multi as $key => $file) {
+                        $params[$key] = '@' . $file;
+                    }
+                    curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
+                    $headers[] = 'Expect: ';
+                } 
+                else 
+                {
+                    curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
+                }
+            }
+            break;
+        case 'DELETE':
+        case 'GET':
+            $method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
+            if (!empty($params)) {
+                $url = $url . (strpos($url, '?') ? '&' : '?')
+                        . (is_array($params) ? http_build_query($params) : $params);
+            }
+            echo $url;
+            break;
+    }
+    curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
+    curl_setopt($ci, CURLOPT_URL, $url);
+    if ($headers) {
+        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
+    }
+
+    $response = curl_exec($ci);
+    curl_close($ci);
+    return $response;
+}

+ 124 - 0
crontab/fetch_goods.php

@@ -0,0 +1,124 @@
+<?php
+
+/*
+* 生成sqe
+* 生成规则:
+ 加密的当前时间戳(精确到秒)+4位随机数 18位数字(yyyymmddhhmmss+rrrr)
+ 加密算法为 字符串反向 后 各偶数位x=9-x
+ 2015-0106-010203-1234
+ ->4[3]2[1]3[0]2[0]1[0]6[0]1[0]5[1]0[2]
+ ->5[3]7[1]6[0]7[0]8[0]3[0]8[0]4[1]9[2]
+*/
+
+require_once (BASE_CORE_PATH . '/framework/function/http.php');
+
+function init_sqe()
+{	
+    date_default_timezone_set('Etc/GMT-8');
+	$d =  date('YmdHis',time());
+	$r = rand(1000,9999); 
+	$str = strrev($d.$r);
+	$str2 = "";
+	for ($i=0; $i < strlen($str); $i++)
+	{ 
+		if($i % 2 == 0) {
+ 			$str2 = $str2.(intval(9-$str[$i]));
+		} else {
+			$str2 = $str2.$str[$i];
+		}
+	}
+	return $str2;
+}
+
+class page_req
+{
+    private $total = '10';
+    private $leftcount;
+    
+    private $items = array();
+   
+    static private $page_url = 'http://crm-api.lrlz.com/api_app/api_main.py';
+    static private $page_method = 'api.info.item.list';
+    static private $item_method = 'api.info.item.get';
+    
+    public function proc_page($shopname,$date)
+    {
+        $curpage = 1;
+        $pagesize = 10;
+        
+        do
+        {
+            $params = array('seq' => init_sqe(), 
+                'method' => page_req::$page_method, 
+                'page' => $curpage, 
+                'pagesize' => $pagesize,
+                'nick' => $shopname,
+                'jdp_modified' => $date);
+        
+            $resp = http_request(page_req::$page_url,$params);
+            if(empty($resp)) {
+                break;
+            } 
+            
+            $data = $this->parse_page($resp);
+            if(empty($data)) {
+                break;
+            } 
+            
+            $curpage += 1;
+            $this->leftcount -= count($data);
+            
+            foreach ($data as $num_iid) {
+                echo "$num_iid\r\n";
+                $item = $this->req_item($num_iid);
+            }
+              
+        } while($this->leftcount > 0);       
+    } 
+    
+    private function req_item($num_iid)
+    {
+    	$params = array('seq' => init_sqe(), 
+    	                'method' => page_req::$item_method, 
+    	                'num_iid' => $num_iid);
+    	$resp = http_request(page_req::$page_url,$params);
+        $str = iconv("gbk","utf-8",$resp);
+        
+        echo $str;
+
+    	return json_decode($str, true);         
+    }
+
+    private function parse_page($body)
+    {
+        $data = json_decode($body, true);    
+
+    	if($data['success']=="true")
+    	{
+    		$this->total = intval(trim($data['total']));
+            $this->curpage = intval(trim($data["page"]));
+            
+            if(!isset($this->leftcount)) {
+                $this->leftcount = $this->total;
+            }
+            
+    		$infos = $data["info"];
+    		$count = count($infos);
+            
+            $items = array();
+    		for ($i = 0; $i < $count; $i++) {
+                $num_iid = $infos[$i]['num_iid'];
+                array_push($items, $num_iid);
+    		}    		
+            return $items;
+    	}else{
+    		return NULL;
+    	}
+    } 
+}
+
+$req = new page_req;
+
+$date = "2015-10-15";
+$req->proc_page("丽人丽妆官方旗舰店",$date,1,10);
+