tmrequest.php 4.6 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()
  21. {
  22. $items = $this->tm_shop->field('shop_id,shop_name,appKey,sercetCode,sessionKey')->limit(false)->select();
  23. foreach($items as $item)
  24. {
  25. echo "Inof: shop id =".$item["shop_id"]." name=".$item["shop_name"]." appKey=".$item['appKey']." sercetCode=".$item['sercetCode']." sessionKey=".$item['sessionKey']."\r\n";
  26. $this->appkey = $item['appKey'];
  27. $this->secretKey = $item['sercetCode'];
  28. $this->sessionKey = $item['sessionKey'];
  29. $shop_id = $item["shop_id"];
  30. $sale_items = $this->get_onsale();
  31. if(empty($sale_items)) {
  32. echo "Error: get product info from taobao err where "."shop id =".$item["shop_id"]." name=".$item["shop_name"]."\r\n";
  33. }
  34. else
  35. {
  36. foreach($sale_items as $sale_item)
  37. {
  38. $num_iid = $sale_item->num_iid;
  39. $issale = $sale_item->approve_status == "onsale" ? true : false;
  40. if($issale)
  41. {
  42. $body = $this->get_body($num_iid);
  43. $desc = $this->get_desc($num_iid);
  44. if(!empty($body)) {
  45. $this->store_data($body,$desc,$shop_id);
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. //添加基础数据。
  53. private function store_data($body,$desc,$shop_id)
  54. {
  55. $response = json_decode($body, true);
  56. $item = &$response['item_seller_get_response']['item'];
  57. if($item['approve_status'] != 'onsale') {
  58. return;
  59. }
  60. $cid = $item['cid'];
  61. $num_iid = $item['num_iid'];
  62. $title = $item['title'];
  63. //把数据添加到商品临时表里面
  64. $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));
  65. if(!$iid) {
  66. Log::record("Can not insert num_iid=$num_iid into tm_goods table.");
  67. }
  68. }
  69. private function get_body($num_iid)
  70. {
  71. $fields = 'cid,seller_cids,props,input_pids,input_str,pic_url,num,list_time,delist_time,stuff_status,location,
  72. has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,auto_repost,approve_status,postage_id,product_id,
  73. item_img, is_virtual,is_taobao,is_ex,is_timing,
  74. 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,
  75. num_iid,title,nick,type,sku,
  76. props_name,created,promoted_service,is_lightning_consignment,is_fenxiao,auction_point,property_alias,volume,
  77. sell_point,valid_thru,outer_id,
  78. barcode,sold_quantity,price,post_fee,express_fee,ems_fee,global_stock_type,global_stock_country,prop_img';
  79. //desc wireless_desc wap_desc
  80. $c = new TopClient;
  81. $c->appkey = $this->appkey;
  82. $c->secretKey = $this->secretKey;
  83. $req = new ItemSellerGetRequest;
  84. $req->setFields($fields);
  85. $req->setNumIid($num_iid);
  86. $resp = $c->execute($req,$body,$this->sessionKey);
  87. if(!empty($resp)) {
  88. $item = &$resp->item;
  89. if(!empty($item)) {
  90. return $body;
  91. }
  92. }
  93. return false;
  94. }
  95. private function get_desc($num_iid)
  96. {
  97. $c = new TopClient;
  98. $c->appkey = $this->appkey;
  99. $c->secretKey = $this->secretKey;
  100. $req = new ItemSellerGetRequest;
  101. $req->setFields("desc");
  102. $req->setNumIid($num_iid);
  103. $resp = $c->execute($req,$body,$this->sessionKey);
  104. if(!empty($resp)) {
  105. $item = &$resp->item;
  106. if(!empty($item)) {
  107. return $item->desc;
  108. }
  109. }
  110. return false;
  111. }
  112. private function get_onsale()
  113. {
  114. $c = new TopClient;
  115. $c->appkey = $this->appkey;
  116. $c->secretKey = $this->secretKey;
  117. $req = new ItemsOnsaleGetRequest;
  118. $req->setFields('approve_status,num_iid');
  119. $resp = $c->execute($req,$body,$this->sessionKey);
  120. if(!empty($resp)) {
  121. $items = $resp->items->item;
  122. return $items;
  123. } else {
  124. return NULL;
  125. }
  126. }
  127. }