Asos.class.php 2.79 KB
<?php
/**
 * 
 * http://www.asos.com(国外的)速度有点慢1.4-4s,有时取不到数据
 *
 */
class Util_Product_Api_Asos extends Util_Product_Api
{
    public function getData($url)
    {
    	if(preg_match('@^http://www.asos.com@i', $url))
    	{
			$this->getHttpData($url);
			
    	}
   		else
   		{
   			$this->getHttpsData($url);
   		}
    }
    
    private function getHttpData($url)
    {
    	$data = Util_Product_ApiConnect::get($url.'&r=2', array(
    			CURLOPT_ENCODING => 'gzip', CURLOPT_TIMEOUT_MS => 7000,
    			CURLOPT_HTTPHEADER=> array
    			(
    					'Accept-Charset: GB-2312,utf-8;q=0.7,*;q=0.7',
    					'Accept-Language: zh-CN,zh;q=0.5',
    			)
    	));
    	if(!empty($data))
    	{
    		@phpQuery::newDocument($data);
    		self::$product->name = pq('#ctl00_ContentMainPage_ctlBreadCrumbs_lblBreadCrumbs')->text();
    		self::$product->price = str_replace('£','', pq('#ctl00_ContentMainPage_ctlSeparateProduct_lblProductPrice')->text());
    		self::$product->img = pq('#ctl00_ContentMainPage_imgMainImage')->attr('src');
    		self::$product->unit = 'GBP';
    		$length = pq('.productThumbnails')->find('img')->length();
    		$imgs = array();
    		for($i = 0;$i<$length; $i++)
    		{
    			$img =  pq('.productThumbnails')->find('img')->eq($i)->attr('src');
    			if(!empty($img))
    			{
    				$imgs[] = str_replace('s.jpg', 'xl.jpg', $img);
    			}
    		}
    		self::$product->imgs = array_values(array_unique($imgs));
    	}
    	else
    	{
    		throw new Util_Product_ApiException('请输入正确的商品地址!');
    	}
    	
    }
    
    private function getHttpsData($url)
    {
    	$data = Util_Product_ApiConnect::get($url, array(
    			CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false,
    			CURLOPT_ENCODING => 'gzip', CURLOPT_TIMEOUT => 10,
    			CURLOPT_HTTPHEADER=> array
    			(
    					'Accept-Charset: GB-2312,utf-8;q=0.7,*;q=0.7',
    					'Accept-Language: zh-CN,zh;q=0.8',
    			)
    	));
    	if(!empty($data))
    	{
    		@phpQuery::newDocument($data);
    		$doc = pq('#content');
    		self::$product->name = $doc->find('#description-panel')->find('h2')->text();
    		self::$product->price = $doc->find('meta[itemprop="price"]')->attr('content');
    		self::$product->unit = $doc->find('meta[itemprop="priceCurrency"]')->attr('content');
    		$length = $doc->find('#additionalViews')->find('li')->length();
    		$imgs = array();
    		for($i = 0;$i<$length; $i++)
    		{
    			$imgs[] = str_replace('_small','_large', $doc->find('#additionalViews')->find('img')->eq($i)->attr('src'));
    		}
    		self::$product->imgs = array_values(array_unique($imgs));
    		self::$product->img = self::$product->imgs[0];
    	}
    	else
    	{
    		throw new Util_Product_ApiException('请输入正确的商品地址!');
    	}
    	
    }

}