Taobao.class.php
5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
require_once dirname(__FILE__).'/../lib/taobao/TopSdk.php';
class Util_Product_Api_Taobao extends Util_Product_Api
{
private static $appKey = '21427790';
private static $appSecret = '24a4b6b0695232497756d0015daf49c2';
private static $appKey_tbk = '21488996';
private static $appSecret_tbk = '909c134be549b4cbf7dd2336c03e3b1f';
private static $taobaokeId = '35220459';
public function getData($url)
{
if(preg_match('@tmall.com@i', $url))
{
return self::getDataByTmall($url);
}
else
{
return self::getDataByTaoBao($url);
}
$numIid = self::getNumIid($url);
$sessionKey = '';
$c = new TopClient();
$c->appkey = self::$appKey;
$c->secretKey = self::$appSecret;
$req = new ItemGetRequest();
$req->setFields("detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual");
$req->setNumIid($numIid);
$resp = $c->execute($req, $sessionKey);
if(isset($resp->item))
{
//下架时间
if(strtotime($resp->item->delist_time)>= strtotime(date('Y-m-d', time())))
{
self::$product->name = (string) $resp->item->title;
self::$product->img = (string) $resp->item->pic_url;
self::$product->price = (string) $resp->item->price;
for($i = 0; $i<10;$i++)
{
if(isset($resp->item->item_imgs->item_img[$i]->url))
{
self::$product->imgs[] = (string)$resp->item->item_imgs->item_img[$i]->url;
}
}
self::$product->imgs = array_values(array_unique(self::$product->imgs));
}
else
{
throw new Util_Product_ApiException('这个商品已下架了!');
}
}
else
{
throw new Util_Product_ApiException('请输入正确的商品地址!');
}
}
/**
* 获取商品信息
*/
private static function getNumIid($url)
{
preg_match_all('@mallstItemId=(.*)&.+@isU', $url, $out, PREG_SET_ORDER);
if(empty($out))
{
preg_match_all('@id=(.*)&.+@isU', $url, $out, PREG_SET_ORDER);
if(empty($out))
{
preg_match_all('@id=(.*)@isu', $url, $out, PREG_SET_ORDER);
}
}
if(empty($out))
{
throw new Util_Product_ApiException('请输入正确的商品地址!');
}
return (double)$out[0][1];
}
/**
* 获取淘宝客URL
* @param unknown_type $id
*/
public static function getTaobaokeUrl($url)
{
$numIid = self::getNumIid($url);
$c = new TopClient;
$c->appkey = self::$appKey_tbk;
$c->secretKey = self::$appSecret_tbk;
$req = new TaobaokeItemsDetailGetRequest;
$req->setFields("click_url");
$req->setPid(self::$taobaokeId);
$req->setNumIids($numIid);
$resp = $c->execute($req);
if(isset($resp->taobaoke_item_details)
&&isset($resp->taobaoke_item_details->taobaoke_item_detail)
&&isset($resp->taobaoke_item_details->taobaoke_item_detail[0]))
{
return $resp->taobaoke_item_details->taobaoke_item_detail[0]->click_url;
}
else
{
return $url;
}
}
/**
* 直接获取淘宝数据
*
* @param string $url
*/
public static function getDataByTaoBao($url)
{
$data = Util_Product_ApiConnect::get($url);
if(!empty($data))
{
phpQuery::$defaultCharset = 'gbk';
@phpQuery::newDocument($data);
$length = pq('#J_UlThumb')->find('li')->length();
for($i=0; $i<$length; $i++)
{
self::$product->imgs[] = str_replace('40x40','310x310', trim(pq('#J_UlThumb')->find('img')->eq($i)->attr('data-src')));
}
self::$product->img = self::$product->imgs[0];
self::$product->name = trim(pq('#detail')->find('.tb-detail-hd')->text());
self::$product->price = trim(pq('#J_StrPriceModBox')->find('.tb-rmb-num')->text());
}
else
{
throw new Util_Product_ApiException('请输入正确的商品地址!');
}
}
/**
* 直接获取天猫数据
*
* @param string $url
*/
public static function getDataByTmall($url)
{
$data = Util_Product_ApiConnect::get($url, array(CURLOPT_FOLLOWLOCATION => true));
preg_match("@'reservePrice' : '(.+)',@i", $data, $out);
if(!empty($data) && !empty($out))
{
phpQuery::$defaultCharset = 'gbk';
@phpQuery::newDocument($data);
$doc = pq('#detail');
$length = $doc->find('#J_UlThumb')->find('img')->length();
for($i=0; $i<$length; $i++)
{
$img = trim($doc->find('#J_UlThumb')->find('img')->eq($i)->attr('src'));
self::$product->imgs[] = preg_replace('@_60x60.+\.jpg@', '', $img);
}
self::$product->img = self::$product->imgs[0];
self::$product->name = trim($doc->find('h3')->text());
self::$product->price = $out[1];
}
else
{
throw new Util_Product_ApiException('请输入正确的商品地址!');
}
}
}