Taobao.class.php 5.44 KB
<?php
require_once dirname(__FILE__).'/../lib/taobao/TopSdk.php';
class Util_Product_Api_Taobao extends Util_Product_Api
{
    private static $appKey = '21427790';
    private static $appSecret = '24a4b6b0695232497756d0015daf49c2';
    
    private static $appKey_tbk = '21488996';
    private static $appSecret_tbk = '909c134be549b4cbf7dd2336c03e3b1f';
    private static $taobaokeId = '35220459';
    
    public function getData($url)
    {
    	if(preg_match('@tmall.com@i', $url))
    	{
    		return self::getDataByTmall($url);
    	}
    	else
    	{
    		return self::getDataByTaoBao($url);
    	}
        $numIid = self::getNumIid($url);
        $sessionKey = '';
        $c = new TopClient();
        $c->appkey = self::$appKey;
        $c->secretKey = self::$appSecret;
        $req = new ItemGetRequest();
        $req->setFields("detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual");
        $req->setNumIid($numIid);
        $resp = $c->execute($req, $sessionKey);  
        
        if(isset($resp->item))
        {
        	//下架时间
        	if(strtotime($resp->item->delist_time)>= strtotime(date('Y-m-d', time())))
        	{
        		self::$product->name = (string) $resp->item->title;
        		self::$product->img = (string) $resp->item->pic_url;
        		self::$product->price = (string) $resp->item->price;
        		for($i = 0; $i<10;$i++)
        		{
        			if(isset($resp->item->item_imgs->item_img[$i]->url))
        			{
        				self::$product->imgs[] = (string)$resp->item->item_imgs->item_img[$i]->url;
        			}
        		}
        		self::$product->imgs = array_values(array_unique(self::$product->imgs));
        	}
        	else
        	{
        		throw new Util_Product_ApiException('这个商品已下架了!');
        	}

        }
        else
        {
            throw new Util_Product_ApiException('请输入正确的商品地址!');
        }
    }
    
    /**
     * 获取商品信息
     */
    private static function getNumIid($url)
    {
        preg_match_all('@mallstItemId=(.*)&.+@isU', $url, $out, PREG_SET_ORDER);
        if(empty($out))
        {
            preg_match_all('@id=(.*)&.+@isU', $url, $out, PREG_SET_ORDER);
            if(empty($out))
            {
                preg_match_all('@id=(.*)@isu', $url, $out, PREG_SET_ORDER);
            }
        }
        if(empty($out))
        {
            throw new Util_Product_ApiException('请输入正确的商品地址!');
        }
        return (double)$out[0][1];
    }
    
    /**
     * 获取淘宝客URL
     * @param unknown_type $id
     */
    public static function getTaobaokeUrl($url)
    {
        $numIid = self::getNumIid($url);
        $c = new TopClient;
        $c->appkey = self::$appKey_tbk;
        $c->secretKey = self::$appSecret_tbk;
        $req = new TaobaokeItemsDetailGetRequest;
        $req->setFields("click_url");
        $req->setPid(self::$taobaokeId);
        $req->setNumIids($numIid);
        $resp = $c->execute($req);
        if(isset($resp->taobaoke_item_details)
                &&isset($resp->taobaoke_item_details->taobaoke_item_detail)
                &&isset($resp->taobaoke_item_details->taobaoke_item_detail[0]))
        {
            return $resp->taobaoke_item_details->taobaoke_item_detail[0]->click_url;
        }
        else
        {
            return $url;
        }
    }
    
    /**
     * 直接获取淘宝数据
     * 
     * @param string $url
     */
    public static function getDataByTaoBao($url)
    {
    	$data = Util_Product_ApiConnect::get($url);
    	if(!empty($data))
    	{
    		phpQuery::$defaultCharset = 'gbk';
    		@phpQuery::newDocument($data);
    		$length = pq('#J_UlThumb')->find('li')->length();
    		for($i=0; $i<$length; $i++)
    		{
    			self::$product->imgs[] = str_replace('40x40','310x310', trim(pq('#J_UlThumb')->find('img')->eq($i)->attr('data-src')));
    		}
    		self::$product->img = self::$product->imgs[0];
    		self::$product->name =  trim(pq('#detail')->find('.tb-detail-hd')->text());
    		self::$product->price =  trim(pq('#J_StrPriceModBox')->find('.tb-rmb-num')->text());
    	}
    	else
    	{
    		throw new Util_Product_ApiException('请输入正确的商品地址!');
    	}
    }
    
    /**
     * 直接获取天猫数据
     *
     * @param string $url
     */
    public static function getDataByTmall($url)
    {
    	$data = Util_Product_ApiConnect::get($url, array(CURLOPT_FOLLOWLOCATION => true));
    	preg_match("@'reservePrice' : '(.+)',@i", $data, $out);
    	if(!empty($data) && !empty($out))
    	{
    		phpQuery::$defaultCharset = 'gbk';
    		@phpQuery::newDocument($data);
    		$doc = pq('#detail');
    		$length =  $doc->find('#J_UlThumb')->find('img')->length();
    		for($i=0; $i<$length; $i++)
    		{
    			$img = trim($doc->find('#J_UlThumb')->find('img')->eq($i)->attr('src'));
    			self::$product->imgs[] = preg_replace('@_60x60.+\.jpg@', '', $img);
    		}
    		self::$product->img = self::$product->imgs[0];
    		self::$product->name =  trim($doc->find('h3')->text());
    		self::$product->price =  $out[1];
    	}
    	else
    	{
    		throw new Util_Product_ApiException('请输入正确的商品地址!');
    	}
    }
    

}