Paipai.class.php 2.19 KB
<?php
/**
 * 
 * http://www.paipai.com/
 * 
 *
 *http://api.paipai.com/item/getItem.xhtml?format=xml&itemCode=1D60F63200000000040100000516F187&needParseAttr=1&needDetailInfo=0&needExtendInfo=1
 */
class Util_Product_Api_Paipai extends Util_Product_Api
{
    private static $xmlUrl = 'http://api.paipai.com/item/getItem.xhtml?format=xml&itemCode={id}&needParseAttr=1&needDetailInfo=0&needExtendInfo=1';
    public function getData($url)
    {
        preg_match_all('@/\d/(.*)-@isU', $url, $out, PREG_SET_ORDER);
        if(empty($out))
        {
            preg_match_all('@/.+/(.*)\?@isU', $url, $out, PREG_SET_ORDER);
        }
        if(empty($out))
        {
        	//xxx.paipai.com/id
        	preg_match_all('@/.+/(.*)@',$url, $out, PREG_SET_ORDER);
        	
        }
        if(!empty($out))
        {
            $data = Util_Product_ApiConnect::get(str_replace('{id}', $out[0][1], self::$xmlUrl));
            $xmlData = @simplexml_load_string($data);
            if(empty($xmlData) || empty($data))
            {
                throw new Util_Product_ApiException('请输入正确的商品地址!');
            }
            self::$product->name = (string)$xmlData->itemName;
            self::$product->price = sprintf('%0.2f', (string)$xmlData->itemPrice/100.0);
            self::$product->img = (string)$xmlData->picLink;
            for($i = 0; $i<10; $i++)
            {
            	$picLink = 'picLink'.$i;
            	if(isset($xmlData->$picLink))
            	{
            		self::$product->imgs[] = (string)$xmlData->$picLink;
            	}
            }
            self::$product->imgs = array_values(array_unique(self::$product->imgs));
            /*foreach($xmlData->extendList->extendInfo as $extendInfo)
            {
                if(isset($extendInfo->extendName) && (string)$extendInfo->extendName === 'SCENE_PRICE')
                {
                    //新价格
                    $info = explode('|', (string) $extendInfo->extendValue);
                    self::$product->price = sprintf('%0.2f', $info[2]/100.0);
                }
            }*/
        }
        else
        {
            throw new Util_Product_ApiException('请输入正确的商品地址!');
        }
    }
}