tmrequest.php 4.4 KB

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