page_request.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 15/11/3
  6. * Time: 下午7:31
  7. * 从杭州那边同步获取数据,
  8. */
  9. require_once (BASE_CORE_PATH . '/framework/function/http.php');
  10. class page_req
  11. {
  12. private $total = '10';
  13. private $leftcount;
  14. private $dispatch = NULL;
  15. static private $page_url = 'http://crm-api.lrlz.com/api_app/api_main.py';
  16. static private $page_method = 'api.info.item.list';
  17. static private $item_method = 'api.info.item.get';
  18. public function __construct() {
  19. $this->dispatch = new pretreat_tmdata();
  20. }
  21. public function proc_page($shopname,$date,$method)
  22. {
  23. $curpage = 1;
  24. $pagesize = 10;
  25. do
  26. {
  27. $params = array('seq' => $this->init_sqe(),
  28. 'method' => page_req::$page_method,
  29. 'page' => $curpage,
  30. 'pagesize' => $pagesize,
  31. 'nick' => $shopname,
  32. 'jdp_modified' => $date);
  33. $resp = http_request(page_req::$page_url,$params);
  34. if(empty($resp)) {
  35. break;
  36. }
  37. $data = $this->parse_page($resp);
  38. if(empty($data)) {
  39. break;
  40. }
  41. $curpage += 1;
  42. $this->leftcount -= count($data);
  43. foreach ($data as $num_iid) {
  44. echo "$num_iid\r\n";
  45. $body = $this->req_item($num_iid);
  46. $this->dispatch->$method($body);
  47. }
  48. } while($this->leftcount > 0);
  49. }
  50. private function req_item($num_iid)
  51. {
  52. $params = array('seq' => $this->init_sqe(),
  53. 'method' => page_req::$item_method,
  54. 'num_iid' => $num_iid);
  55. $resp = http_request(page_req::$page_url,$params);
  56. $str = iconv("gbk","utf-8",$resp);
  57. return $str;
  58. }
  59. private function parse_page($body)
  60. {
  61. $data = json_decode($body, true);
  62. if($data['success']=="true")
  63. {
  64. $this->total = intval(trim($data['total']));
  65. $this->curpage = intval(trim($data["page"]));
  66. if(!isset($this->leftcount)) {
  67. $this->leftcount = $this->total;
  68. }
  69. $infos = $data["info"];
  70. $count = count($infos);
  71. $items = array();
  72. for ($i = 0; $i < $count; $i++) {
  73. $num_iid = $infos[$i]['num_iid'];
  74. array_push($items, $num_iid);
  75. }
  76. return $items;
  77. }else{
  78. return NULL;
  79. }
  80. }
  81. /*
  82. * 生成sqe
  83. * 生成规则:
  84. 加密的当前时间戳(精确到秒)+4位随机数 18位数字(yyyymmddhhmmss+rrrr)
  85. 加密算法为 字符串反向 后 各偶数位x=9-x
  86. 2015-0106-010203-1234
  87. ->4[3]2[1]3[0]2[0]1[0]6[0]1[0]5[1]0[2]
  88. ->5[3]7[1]6[0]7[0]8[0]3[0]8[0]4[1]9[2]
  89. */
  90. private function init_sqe()
  91. {
  92. date_default_timezone_set('Etc/GMT-8');
  93. $d = date('YmdHis',time());
  94. $r = rand(1000,9999);
  95. $str = strrev($d.$r);
  96. $str2 = "";
  97. for ($i=0; $i < strlen($str); $i++)
  98. {
  99. if($i % 2 == 0) {
  100. $str2 = $str2.(intval(9-$str[$i]));
  101. } else {
  102. $str2 = $str2.$str[$i];
  103. }
  104. }
  105. return $str2;
  106. }
  107. }