page_request.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. }
  49. while($this->leftcount > 0);
  50. }
  51. private function req_item($num_iid)
  52. {
  53. $params = array('seq' => $this->init_sqe(),
  54. 'method' => page_req::$item_method,
  55. 'num_iid' => $num_iid);
  56. $resp = http_request(page_req::$page_url,$params);
  57. $str = iconv("gbk","utf-8",$resp);
  58. return $str;
  59. }
  60. private function parse_page($body)
  61. {
  62. $data = json_decode($body, true);
  63. if($data['success']=="true")
  64. {
  65. $this->total = intval(trim($data['total']));
  66. $this->curpage = intval(trim($data["page"]));
  67. if(!isset($this->leftcount)) {
  68. $this->leftcount = $this->total;
  69. }
  70. $infos = $data["info"];
  71. $count = count($infos);
  72. $items = array();
  73. for ($i = 0; $i < $count; $i++) {
  74. $num_iid = $infos[$i]['num_iid'];
  75. array_push($items, $num_iid);
  76. }
  77. return $items;
  78. }else{
  79. return NULL;
  80. }
  81. }
  82. /*
  83. * 生成sqe
  84. * 生成规则:
  85. 加密的当前时间戳(精确到秒)+4位随机数 18位数字(yyyymmddhhmmss+rrrr)
  86. 加密算法为 字符串反向 后 各偶数位x=9-x
  87. 2015-0106-010203-1234
  88. ->4[3]2[1]3[0]2[0]1[0]6[0]1[0]5[1]0[2]
  89. ->5[3]7[1]6[0]7[0]8[0]3[0]8[0]4[1]9[2]
  90. */
  91. private function init_sqe()
  92. {
  93. date_default_timezone_set('Etc/GMT-8');
  94. $d = date('YmdHis',time());
  95. $r = rand(1000,9999);
  96. $str = strrev($d.$r);
  97. $str2 = "";
  98. for ($i=0; $i < strlen($str); $i++)
  99. {
  100. if($i % 2 == 0) {
  101. $str2 = $str2.(intval(9-$str[$i]));
  102. } else {
  103. $str2 = $str2.$str[$i];
  104. }
  105. }
  106. return $str2;
  107. }
  108. }