<?php
/**
 *      [QKSHOP] (C) QIKEWEB Inc.
 *      This is NOT a freeware, use is subject to license terms
 */

class PaipaiApi{
	function index(){
	
	}

	function getInfo($iid){
		$ret = array();
		$item = $this->getItem($iid);  //print_r( $item);exit;
		$goods = $this->goodsInfo($iid);	//errorCode
		$goods = $goods['CpsQueryResult']['cpsSearchCommData'];
		//print_r($goods);
		$ret['click_url'] = $goods['sClickUrl'];
		$ret['item']['desc'] = $item['detailInfo'];
		$ret['item']['nick'] = $item['sellerUin'];
		$ret['item']['detail_url'] = $goods['sCommUrl'];
		$ret['item']['pic_url'] = $goods['sCommImgUrl'];
		$ret['item']['price'] = $goods['dwPrice']/100;
		$ret['item']['title'] = $goods['sTitle'];
		$ret['item']['num'] = $goods['dwNum'];
		$ret['item']['num_iid'] = $goods['sRespCommId'];
		$ret['shop_click_url'] = '';
		$ret['promotionInfo'] = '';
		return $ret;
	}
	function getItem($iid){
		$param = array(
			"url"=>"http://api.paipai.com/item/getItem.xhtml?charset=utf-8",
			"format"=>"json",
			"pureData"=>"1",
			"itemCode"=>$iid,
			"itemLocalCode"=>"",
			"needParseAttr"=>"0",
			"needDetailInfo"=>"1",
			"needExtendInfo"=>"0"
		);
		//print_r($param);
		return $this->http($param);
	
	}

	function checksign(){
		$url = "http://api.paipai.com/module/action.xhtml";
		$param = array(
			"charset"=>"utf-8",
			"xxx"=>"中文",
			"zzz"=>"2",
			"yyy"=>"3"
		);
		$secretKey = "33230000e9030ghjtsfgh0564a0ce621";
		$sign = "43cc81fd5a86131ae10052e03ce008d3";
		$gbksign = "32d8585d69686a707426f57c6adec0ea";
		echo "原参数："; print_r($param);
		echo "<br/>";
		ksort($param);
		reset($param);
		echo "排序后参数："; print_r($param);
		$paramstr = '';
		foreach($param as $key=>$val){
			$paramstr .= $key.$val;
		}
		$csign = md5("module.action".$paramstr.$secretKey);
		echo "<br>正常加密结果：".$sign;
		echo "<br>本次加密结果：".$csign;
		echo "<br>GBK加密结果：".$gbksign;
	}

	function search(){
		$param = array(
			"url"=>"http://api.paipai.com/cps/cpsCommSearch.xhtml",
			"uin"=>"228759217",
			""=>"",
			""=>"",
			""=>"",
			""=>"",
			""=>""
		);
	}

	function goodsInfo($iid){
		$param = array(
			"url"=>"http://api.paipai.com/cps/cpsCommQueryAction.xhtml?charset=utf-8",// charset参数用于指定request和response的编码，默认值为gbk。注意：charset参数如果需要提交，必须出现在url中，否则不会生效。例如 http://api.paipai.com/item/getItem.xhtml?charset=utf-8
			"format"=>"json",																	//输出格式,默认值为json。
			"pureData"=>"1",																	//默认值为0。该参数只有format=json时才有意义。pureData=1时，json格式只会包含一个纯粹的json数据对象，而不会包含其他js代码，如try...catch、回调函数等。非paipai.com和qq.com的web页面引用时，pureData必定为1。
			"commId"=>$iid,			//商品ID	3263F63200000000040100001EE17985
			"userId"=>"38561",																	//推广者ID
			"channelId"=>"",																		//渠道ID
			"materialId"=>"",																		//素材ID
			"adPosition"=>"",																	//广告位
			"outInfo"=>"",																			//附加信息
			"ptag"=>""																				//ptag(可选项) 
		);
		return $this->http($param);
	}

	function http($param){
		$url = $param['url'];
		if(!$url){return false;}
		array_shift($param);
		$http = new http($url);
		$http->setPost($param);
		$http->createCurl();
		$ret = $http->responseText();
		$ret = json_decode($ret,true);
		return $ret;
	}
	

}
/*

Array
(
    [CpsQueryResult] => Array
        (
            [errorCode] => 0
            [errorMessage] => Success
            [rootName] => api
            [cpsSearchCommData] => Array
                (
                    [dwUin] => 855008050
                    [dwIsCpsFlag] => 1
                    [dwActiveFlag] => 1
                    [sRespCommId] => 3263F63200000000040100001EE17985
                    [sTitle] => 慕倩 高腰提臀超柔软糖果色女士三角内裤7条装 0913
                    [dwState] => 2
                    [dwPrimaryCmm] => 0
                    [dwPrimaryRate] => 0
                    [dwClassRate] => 150
                    [dwBaseClassId] => 27158
                    [dwNewType] => 1
                    [dwDealType] => 1
                    [dwSendType] => 0
                    [dwTransportPriceType] => 1
                    [dwNormalMailPrice] => 0
                    [dwExpressMailPrice] => 0
                    [dwEmsMailPrice] => 0
                    [dwWeight] => 0
                    [dwLeafClassId] => 20565
                    [dwPrice] => 15960
                    [dwMarketPrice] => 0
                    [dwRedPrice] => 0
                    [sCommImgUrl] => http://img2.paipaiimg.com/27e8eb6b/item-515BC5B7-3263F63200000000040100001EE17985.0.jpg
                    [dwNum] => 1561
                    [dwPayNum] => 838
                    [dwTotalPayNum] => 838
                    [dwCurrPayNum] => 0
                    [dwCurrAuctionPrice] => 0
                    [sCommUrl] => http://auction1.paipai.com/3263F63200000000040100001EE17985
                    [sClickUrl] => http://te.paipai.com/tws/etgcl/click?fu=http%3A%2F%2Fauction1.paipai.com%2F3263F63200000000040100001EE17985.3000&pps=etg.0_38561_0_0
                )

        )

)

*/
?>