tmrequest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 15/11/3
  6. * Time: 下午7:51
  7. */
  8. class tmrequest
  9. {
  10. private $appkey;
  11. private $secretKey;
  12. private $sessionKey;
  13. private $tm_shop;
  14. private $tm_goods;
  15. public function __construct()
  16. {
  17. $this->tm_shop = Model('tm_shop');
  18. $this->tm_goods = Model('tm_goods');
  19. }
  20. public function proc_one($shopid)
  21. {
  22. $items = $this->tm_shop->where(array('shop_id' => $shopid))->field('shop_id,shop_name,appKey,sercetCode,sessionKey')->limit(false)->select();
  23. $this->request($items);
  24. }
  25. public function proc()
  26. {
  27. $items = $this->tm_shop->field('shop_id,shop_name,appKey,sercetCode,sessionKey')->limit(false)->select();
  28. $this->request($items);
  29. }
  30. //添加基础数据。
  31. private function store_data($body,$desc,$shop_id)
  32. {
  33. $response = json_decode($body, true);
  34. $item = &$response['item_seller_get_response']['item'];
  35. if($item['approve_status'] != 'onsale') {
  36. return;
  37. }
  38. $cid = $item['cid'];
  39. $num_iid = $item['num_iid'];
  40. $title = $item['title'];
  41. //把数据添加到商品临时表里面
  42. $iid = $this->tm_goods->insert(array("num_iid" => $num_iid,'shop_id' => $shop_id,'body' => $body,'cid' => $cid,'product_id' =>$item['product_id'],'title' => $title, 'description' => $desc));
  43. if(!$iid) {
  44. Log::record("Can not insert num_iid=$num_iid into tm_goods table.");
  45. }
  46. }
  47. private function get_body($num_iid)
  48. {
  49. $fields = 'cid,seller_cids,props,input_pids,input_str,pic_url,num,list_time,delist_time,stuff_status,location,
  50. has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,auto_repost,approve_status,postage_id,product_id,
  51. item_img, is_virtual,is_taobao,is_ex,is_timing,
  52. is_3D,score,one_station,second_kill,violation,is_prepay,ww_status,wap_detail_url,cod_postage_id,sell_promise,period_sold_quantity,detail_url,
  53. num_iid,title,nick,type,sku,
  54. props_name,created,promoted_service,is_lightning_consignment,is_fenxiao,auction_point,property_alias,volume,
  55. sell_point,valid_thru,outer_id,
  56. barcode,sold_quantity,price,post_fee,express_fee,ems_fee,global_stock_type,global_stock_country,prop_img';
  57. //desc wireless_desc wap_desc
  58. $c = new TopClient;
  59. $c->appkey = $this->appkey;
  60. $c->secretKey = $this->secretKey;
  61. $req = new ItemSellerGetRequest;
  62. $req->setFields($fields);
  63. $req->setNumIid($num_iid);
  64. $resp = $c->execute($req,$body,$this->sessionKey);
  65. if(!empty($resp)) {
  66. $item = &$resp->item;
  67. if(!empty($item)) {
  68. return $body;
  69. }
  70. }
  71. return false;
  72. }
  73. private function get_desc($num_iid)
  74. {
  75. $c = new TopClient;
  76. $c->appkey = $this->appkey;
  77. $c->secretKey = $this->secretKey;
  78. $req = new ItemSellerGetRequest;
  79. $req->setFields("desc");
  80. $req->setNumIid($num_iid);
  81. $resp = $c->execute($req,$body,$this->sessionKey);
  82. if(!empty($resp))
  83. {
  84. $item = &$resp->item;
  85. if(!empty($item)) {
  86. return $item->desc;
  87. }
  88. }
  89. return false;
  90. }
  91. private function get_onsale()
  92. {
  93. $req = new tm_pagereq($this->appkey,$this->secretKey,$this->sessionKey);
  94. return $req->proc();
  95. }
  96. /**
  97. * @param $items
  98. */
  99. private function request($items)
  100. {
  101. foreach ($items as $item)
  102. {
  103. Log::record("Inof: shop id =" . $item["shop_id"] . " name=" . $item["shop_name"] . " appKey=" . $item['appKey'] . " sercetCode=" . $item['sercetCode'] . " sessionKey=" . $item['sessionKey'],Log::DEBUG);
  104. $this->appkey = $item['appKey'];
  105. $this->secretKey = $item['sercetCode'];
  106. $this->sessionKey = $item['sessionKey'];
  107. $shop_id = $item["shop_id"];
  108. $sale_items = $this->get_onsale();
  109. if (empty($sale_items)) {
  110. Log::record("get product info from taobao err where " . "shop id =" . $item["shop_id"] . " name=" . $item["shop_name"],Log::ERR);
  111. }
  112. else
  113. {
  114. foreach ($sale_items as $sale_item)
  115. {
  116. $num_iid = $sale_item->num_iid;
  117. $issale = $sale_item->approve_status == "onsale" ? true : false;
  118. if ($issale) {
  119. $body = $this->get_body($num_iid);
  120. $desc = $this->get_desc($num_iid);
  121. if (!empty($body)) {
  122. $this->store_data($body, $desc, $shop_id);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }