Paipai.class.php
2.19 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
<?php
/**
*
* http://www.paipai.com/
*
*
*http://api.paipai.com/item/getItem.xhtml?format=xml&itemCode=1D60F63200000000040100000516F187&needParseAttr=1&needDetailInfo=0&needExtendInfo=1
*/
class Util_Product_Api_Paipai extends Util_Product_Api
{
private static $xmlUrl = 'http://api.paipai.com/item/getItem.xhtml?format=xml&itemCode={id}&needParseAttr=1&needDetailInfo=0&needExtendInfo=1';
public function getData($url)
{
preg_match_all('@/\d/(.*)-@isU', $url, $out, PREG_SET_ORDER);
if(empty($out))
{
preg_match_all('@/.+/(.*)\?@isU', $url, $out, PREG_SET_ORDER);
}
if(empty($out))
{
//xxx.paipai.com/id
preg_match_all('@/.+/(.*)@',$url, $out, PREG_SET_ORDER);
}
if(!empty($out))
{
$data = Util_Product_ApiConnect::get(str_replace('{id}', $out[0][1], self::$xmlUrl));
$xmlData = @simplexml_load_string($data);
if(empty($xmlData) || empty($data))
{
throw new Util_Product_ApiException('请输入正确的商品地址!');
}
self::$product->name = (string)$xmlData->itemName;
self::$product->price = sprintf('%0.2f', (string)$xmlData->itemPrice/100.0);
self::$product->img = (string)$xmlData->picLink;
for($i = 0; $i<10; $i++)
{
$picLink = 'picLink'.$i;
if(isset($xmlData->$picLink))
{
self::$product->imgs[] = (string)$xmlData->$picLink;
}
}
self::$product->imgs = array_values(array_unique(self::$product->imgs));
/*foreach($xmlData->extendList->extendInfo as $extendInfo)
{
if(isset($extendInfo->extendName) && (string)$extendInfo->extendName === 'SCENE_PRICE')
{
//新价格
$info = explode('|', (string) $extendInfo->extendValue);
self::$product->price = sprintf('%0.2f', $info[2]/100.0);
}
}*/
}
else
{
throw new Util_Product_ApiException('请输入正确的商品地址!');
}
}
}