Api.class.php 2.59 KB
<?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);
}