Asos.class.php
2.79 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
<?php
/**
*
* http://www.asos.com(国外的)速度有点慢1.4-4s,有时取不到数据
*
*/
class Util_Product_Api_Asos extends Util_Product_Api
{
public function getData($url)
{
if(preg_match('@^http://www.asos.com@i', $url))
{
$this->getHttpData($url);
}
else
{
$this->getHttpsData($url);
}
}
private function getHttpData($url)
{
$data = Util_Product_ApiConnect::get($url.'&r=2', array(
CURLOPT_ENCODING => 'gzip', CURLOPT_TIMEOUT_MS => 7000,
CURLOPT_HTTPHEADER=> array
(
'Accept-Charset: GB-2312,utf-8;q=0.7,*;q=0.7',
'Accept-Language: zh-CN,zh;q=0.5',
)
));
if(!empty($data))
{
@phpQuery::newDocument($data);
self::$product->name = pq('#ctl00_ContentMainPage_ctlBreadCrumbs_lblBreadCrumbs')->text();
self::$product->price = str_replace('£','', pq('#ctl00_ContentMainPage_ctlSeparateProduct_lblProductPrice')->text());
self::$product->img = pq('#ctl00_ContentMainPage_imgMainImage')->attr('src');
self::$product->unit = 'GBP';
$length = pq('.productThumbnails')->find('img')->length();
$imgs = array();
for($i = 0;$i<$length; $i++)
{
$img = pq('.productThumbnails')->find('img')->eq($i)->attr('src');
if(!empty($img))
{
$imgs[] = str_replace('s.jpg', 'xl.jpg', $img);
}
}
self::$product->imgs = array_values(array_unique($imgs));
}
else
{
throw new Util_Product_ApiException('请输入正确的商品地址!');
}
}
private function getHttpsData($url)
{
$data = Util_Product_ApiConnect::get($url, array(
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_ENCODING => 'gzip', CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER=> array
(
'Accept-Charset: GB-2312,utf-8;q=0.7,*;q=0.7',
'Accept-Language: zh-CN,zh;q=0.8',
)
));
if(!empty($data))
{
@phpQuery::newDocument($data);
$doc = pq('#content');
self::$product->name = $doc->find('#description-panel')->find('h2')->text();
self::$product->price = $doc->find('meta[itemprop="price"]')->attr('content');
self::$product->unit = $doc->find('meta[itemprop="priceCurrency"]')->attr('content');
$length = $doc->find('#additionalViews')->find('li')->length();
$imgs = array();
for($i = 0;$i<$length; $i++)
{
$imgs[] = str_replace('_small','_large', $doc->find('#additionalViews')->find('img')->eq($i)->attr('src'));
}
self::$product->imgs = array_values(array_unique($imgs));
self::$product->img = self::$product->imgs[0];
}
else
{
throw new Util_Product_ApiException('请输入正确的商品地址!');
}
}
}