goods_info_by_url.model.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * 根据url获取商品信息
  4. *
  5. *
  6. */
  7. defined('InShopNC') or exit('Access Invalid!');
  8. class goods_info_by_urlModel{
  9. /**
  10. * 根据链接返回商品信息
  11. */
  12. public function get_goods_info_by_url($url)
  13. {
  14. $url_host_name = self::get_url_domain($url);
  15. $store_host_name = self::get_url_domain(SHOP_SITE_URL);
  16. switch ($url_host_name) {
  17. case 'tmall.com':
  18. case 'taobao.com':
  19. if(C('taobao_api_isuse')) {
  20. return self::get_taobao_goods_info_by_url($url);
  21. } else {
  22. return FALSE;
  23. }
  24. break;
  25. case $store_host_name:
  26. return self::get_store_goods_info_by_url($url);
  27. default:
  28. return FALSE;
  29. break;
  30. }
  31. }
  32. /**
  33. * 判断链接合法性
  34. */
  35. public function check_personal_buy_link($url) {
  36. $link_host_name = self::get_url_domain($url);
  37. $store_host_name = self::get_url_domain(SHOP_SITE_URL);
  38. switch ($link_host_name) {
  39. case 'tmall.com':
  40. case 'taobao.com':
  41. if(C('taobao_api_isuse')) {
  42. return TRUE;
  43. } else {
  44. return FALSE;
  45. }
  46. break;
  47. case $store_host_name:
  48. return TRUE;
  49. default:
  50. return FALSE;
  51. break;
  52. }
  53. }
  54. /**
  55. * 获取主域名
  56. */
  57. private function get_url_domain($url) {
  58. $url_parse_array = parse_url($url);
  59. $host = $url_parse_array['host'];
  60. $host_names = explode(".", $host);
  61. $bottom_host_name = $host_names[count($host_names)-2] . "." . $host_names[count($host_names)-1];
  62. return $bottom_host_name;
  63. }
  64. private function get_taobao_goods_info_by_url($url) {
  65. require(BASE_DATA_PATH.DS.'api'.DS.'taobao'.DS.'index.php');
  66. $taobao_api = new taobao_item;
  67. $taobao_goods_info = $taobao_api->fetch($url);
  68. $result = FALSE;
  69. if($taobao_goods_info) {
  70. //处理图片地址
  71. $item_img = (array)$taobao_goods_info['item_imgs'];
  72. $item_img = (array)$item_img['item_img'][0];
  73. $item_img = $item_img['url'];
  74. $url_array = explode('.',$item_img);
  75. $ext = end($url_array);
  76. $item_img = $item_img.'_160x160.'.$ext;
  77. $result = array();
  78. $result['result'] = 'true';
  79. $result['id'] = 0;
  80. $result['url'] = $taobao_goods_info['detail_url'];
  81. $result['price'] = $taobao_goods_info['price'];
  82. $result['storeid'] = 0;
  83. $result['title'] = $taobao_goods_info['title'];
  84. $result['img'] = $item_img;
  85. $result['image'] = $item_img;
  86. $result['type'] = 'taobao';
  87. }
  88. return $result;
  89. }
  90. private function get_store_goods_info_by_url($url) {
  91. $array = parse_url($url);
  92. $goods_id = 0;
  93. if(isset($array['query'])){
  94. // 未开启伪静态
  95. parse_str($array['query'],$arr);
  96. $goods_id = $arr['goods_id'];
  97. }else{
  98. // 开启伪静态
  99. $data = explode('/', $array['path']);
  100. $path = end($data);
  101. $goods_id = preg_replace('/item-(\d+)\.html/i', '$1', $path);
  102. }
  103. if(intval($goods_id) > 0) {
  104. $model = Model('goods');
  105. $goods_info = $model->getGoodsInfoByID(intval($goods_id));
  106. if(!empty($goods_info)) {
  107. $result = array();
  108. $result['result'] = 'true';
  109. $result['id'] = intval($goods_id);
  110. $result['url'] = $url;
  111. $result['price'] = $goods_info['goods_price'];
  112. $result['storeid'] = $goods_info['store_id'];
  113. $result['title'] = $goods_info['goods_name'];
  114. $result['img'] = $goods_info['goods_image'];
  115. $result['image'] = thumb($goods_info, 240);
  116. $result['type'] = 'store';
  117. return $result;
  118. } else {
  119. return FALSE;
  120. }
  121. } else {
  122. return FALSE;
  123. }
  124. }
  125. }