Dangdang.class.php
1.49 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
<?php
/**
* http://www.dangdang.com
*
*/
class Util_Product_Api_Dangdang extends Util_Product_Api
{
public function getData($url)
{
$data = Util_Product_ApiConnect::get($url);
if(!empty($data))
{
phpQuery::$defaultCharset = 'gbk';
@phpQuery::newDocument($data);
$title = iconv('gb2312','utf-8', pq('.h1_title')->find('h1')->html());
if(empty($title))
{
$title = iconv('gb2312','utf-8', pq('.head')->find('h1')->html());
}
$price = pq('#salePriceTag')->text();
$price = str_replace('¥','', $price);
$price = str_replace('¥','', $price);
$img = pq('#largePic')->attr('src');
if(!preg_match('@^http://@', $img))
{
$img = pq('#largePic')->attr('wsrc');
}
self::$product->name = trim(preg_replace('@<span (.*)</span>@','', $title));
self::$product->price = $price;
self::$product->img = $img;
$length = pq("#mainimg_pic")->find('img')->length();
for($i = 0; $i<$length; $i++)
{
self::$product->imgs[] = str_replace('_x', '_w', pq("#mainimg_pic")->find('img')->eq($i)->attr('src'));
}
self::$product->imgs = array_values(array_unique(self::$product->imgs));
}
else
{
throw new Util_Product_ApiException('请输入正确的商品地址!');
}
}
}