Api.class.php
2.59 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
<?php
require_once 'lib/phpQuery.php';
abstract class Util_Product_Api
{
protected static $product;
private static $adapterApi = array();
public static function loadData()
{
self::$adapterApi = array(
'360buy',
'americanapparel',
'asos',
'caomeipai',
'dangdang',
'forever21',
'gap',
'hm',
'itezhop',
'mbaobao',
'mltd',
'nala',
'paipai',
'shopbop',
'yohobuy',
'stylefile',
'taobao',
'vancl',
'xipin',
'yoox',
'zara',
'zozo',
'revolveclothing',
);
}
public static function isShopUrl($url)
{
self::loadData();
$parseUrl = @parse_url($url);
if(isset($parseUrl['host']))
{
foreach(self::$adapterApi as $api)
{
if(preg_match("/" . $api . "/is", $parseUrl['host']))
{
return $api;
}
else if(preg_match('/tmall/is', $parseUrl['host']))
{
return 'taobao';
}
else if(preg_match('/jd.com/is', $parseUrl['host']))
{
return '360buy';
}
else if(preg_match('/wanggou.com/is', $parseUrl['host']))
{
return 'paipai';
}
}
}
return '';
}
private static function regClass($name)
{
$class_prefix = 'Util_Product_Api_';
$class = $class_prefix . ucfirst($name);
if((class_exists($class) && is_subclass_of($class, __CLASS__)))
{
return new $class();
}
else
{
throw new Util_Video_ApiException($class . '类不存在');
}
}
/**
* 获取商品
*
* @param string $url
* @return Util_Product_Data
*/
public static function autoGetData($url)
{
phpQuery::$documents = array();
self::$product = new Util_Product_Data();
$api = self::isShopUrl($url);
try
{
$shopApi = self::regClass($api);
$shopApi->getData($url);
self::$product->source = $url;
self::$product->shop = $api;
return self::$product;
}
catch(Exception $e)
{
return $e->getMessage();
}
}
abstract public function getData($url);
}