Dangdang.class.php 1.49 KB
<?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('请输入正确的商品地址!');
        }
    }
}