<?php
#2014-12-03
#等同于TbData.PHP 将产品数据通过API接口获取和TbData.php取得的结构一样的数据
#将数据统一返回由日本服务器进行翻译
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(100);
date_default_timezone_set('Europe/Moscow');
!headers_sent() && header("Content-type:text/html;charset=utf-8");
class TbApiData{
private $appId='21512960';
private $appSecret='901f71b256f3841e920be005ca3aa6fd';
private $id;
private $start;
public $cost;
#实例化类,可入参产品ID
public function __construct($product_id=0) {
$this->id = $product_id;
$this->start=microtime(true);
}
#返回格式化数据函数
public function formatData($data,$promotion){
#基础数据
$dsgData=new stdClass();
$dsgData->item=new stdClass();
$dsgData->item->num_iid=$data['item_get_response']['item']['num_iid'];
$dsgData->item->cid=$data['item_get_response']['item']['cid'];
$dsgData->item->nick=$data['item_get_response']['item']['nick'];
$dsgData->item->title=$data['item_get_response']['item']['title'];
$dsgData->item->pic_url=$data['item_get_response']['item']['pic_url'];
$dsgData->item->post_fee=$data['item_get_response']['item']['post_fee'];
$dsgData->item->ems_fee=$data['item_get_response']['item']['ems_fee'];
$dsgData->item->price=$data['item_get_response']['item']['price'];
$dsgData->item->num=$data['item_get_response']['item']['num'];
$dsgData->item->location=@implode('.',array_reverse($data['item_get_response']['item']['location']));
$dsgData->item->item_imgs->item_img=array();
#图片数据转化为对象
if(isset($data['item_get_response']['item']['item_imgs']['item_img']) && $data['item_get_response']['item']['item_imgs']['item_img']){
foreach($data['item_get_response']['item']['item_imgs']['item_img'] as $k=>$row){
$dsgData->item->item_imgs->item_img[$k]=(object)$row;
}
}
#要替换的props数据整理
$property_new=array();
if(isset($data['item_get_response']['item']['property_alias']) && $data['item_get_response']['item']['property_alias']){
$property_alias=explode(';',$data['item_get_response']['item']['property_alias']);
foreach($property_alias as $k=>$value){
list($pid,$vid,$vname)=explode(':',$value);
$property_new[$pid.':'.$vid]=$vname;
}
}
#预处理属性图片
$property_img=array();
if(isset($data['item_get_response']['item']['prop_imgs']['prop_img']) && $data['item_get_response']['item']['prop_imgs']['prop_img']){
foreach($data['item_get_response']['item']['prop_imgs']['prop_img'] as $k=>$row){
$property_img[$row['properties']]=$row['url'];
}
}
#取item_attributes 返回props_name由翻译里执行
/*$dsgData->item->item_attributes=array();
if($data['item_get_response']['item']['props_name']){
$props=explode(';',$data['item_get_response']['item']['props_name']);
$prop_arr=array();
foreach($props as $k=>$value){
list($pid,$vid,$pname,$vname)=explode(':',$value);
$key=array_search($pname,$prop_arr);
#是否要替换
$new_key=$pid.':'.$vid;
if(isset($property_new[$new_key])) $vname=$property_new[$new_key];
if($key!==false){
$dsgData->item->item_attributes[$key]->val.=' '.$vname;
}else{
$dsgData->item->item_attributes[$k]=new stdClass();
$dsgData->item->item_attributes[$k]->prop=$pname;
$dsgData->item->item_attributes[$k]->val=$vname;
$prop_arr[$k]=$pname;
}
}
}*/
#价格预拼接
$sku_price=array();
$dsgData->item->promotion_price='';
if(isset($promotion['ump_promotion_get_response']['promotions']['promotion_in_item']['promotion_in_item'][0])){
$dsgData->item->promotion_price=$promotion['ump_promotion_get_response']['promotions']['promotion_in_item']['promotion_in_item'][0]['item_promo_price'];
foreach($promotion['ump_promotion_get_response']['promotions']['promotion_in_item']['promotion_in_item'][0]['sku_id_list']['string'] as $k=>$skuid){
$skuid=(string)$skuid;
$sku_price[$skuid]=$promotion['ump_promotion_get_response']['promotions']['promotion_in_item']['promotion_in_item'][0]['sku_price_list']['price'][$k];
}
}
#props拼接 #同时添加进SKU值
$dsgData->item->props=array();
$dsgData->item->skus->sku=array();
if(isset($data['item_get_response']['item']['skus']) && $data['item_get_response']['item']['skus']){
foreach($data['item_get_response']['item']['skus']['sku'] as $k=>$row){
$prop_arr=explode(';',$row['properties_name']);
#SKU入数据
$sku_add=new stdClass();
$sku_add->price=$row['price'];
$sku_add->properties=$row['properties'];
$sku_add->properties_name= '';
$sku_add->quantity=$row['quantity'];
$sku_add->sku_id =$row['sku_id'];
$skukey=(string)$row['sku_id'];
@$sku_add->promotion_price=$sku_price[$skukey];
$dsgData->item->skus->sku[]=$sku_add;
#PROPS入数据
foreach($prop_arr as $sk=>$prop){
list($pid,$vid,$pname,$vname)=explode(':',$prop);
if(!isset($dsgData->item->props[$pid])){
$dsgData->item->props[$pid]=new stdClass();
$dsgData->item->props[$pid]->cid=$data['item_get_response']['item']['cid'];
#$dsgData->item->props[$pid]->name_zh=
$dsgData->item->props[$pid]->name=$pname;
$dsgData->item->props[$pid]->childs=array();
}
#添加[childs]
if(!isset($dsgData->item->props[$pid]->childs[$vid])){
$childObj=new stdClass();
$childObj->vid=$vid;
#是否要替换
$new_key=$pid.':'.$vid;
if(isset($property_new[$new_key])) $vname=$property_new[$new_key];
#$childObj->name_zh=
$childObj->name=$vname;
$childObj->url=isset($property_img[$new_key])?$property_img[$new_key]:'';
$dsgData->item->props[$pid]->childs[$vid]=$childObj;
}
}
}
}
#添加props_name以供翻译使用
$dsgData->item->props_name=$data['item_get_response']['item']['props_name'];
$dsgData->item->Apidesc=$data['item_get_response']['item']['desc'];
$dsgData->item->desc=$this->clearHtml($data['item_get_response']['item']['desc']);
$dsgData->item->Apidesc=NULL;
$this->cost=microtime(true)-$this->start;
return $dsgData;
}
#总调用接口
public function MethodProduct($product_id){
if(!$product_id) exit('no id');
$this->id=$product_id;
#第一步:调产品信息
$result=$this->MethodItem($product_id);
#第二步:调产品prop
$promotion=$this->MethodProp($product_id);
#测试展示API抓取数据
#print_r($result);print_r($promotion);exit;
#第三步:组合产品结构
$data=$this->formatData($result,$promotion);
#第四步:取回产品RATE和sellerID值
$RateData=$this->getRate();
#print_r($RateData);exit;
#将rate值组合进Data
$data->item->isTmall=$RateData->item->isTmall;
$data->item->seller_id=$RateData->item->seller_id;
if(!isset($data->item->cid) || $data->item->cid) $data->item->cid=$RateData->item->cid;
if(!isset($data->item->shop_id) || $data->item->shop_id) $data->item->shop_id=$RateData->item->shop_id;
if(!isset($data->item->post_fee) || $data->item->post_fee) $data->item->post_fee=$RateData->item->post_fee;
if(!isset($data->item->express_fee) || $data->item->express_fee) $data->item->express_fee=$RateData->item->express_fee;
if(!isset($data->item->ems_fee) || $data->item->ems_fee) $data->item->ems_fee=$RateData->item->ems_fee;
if(!isset($data->item->num) || $data->item->num) $data->item->num=$RateData->item->num;
$data->rate=$RateData->rate;
$RateData=NULL;
return $data;
}
#取产品详细信息接口
public function MethodItem($product_id=0){
!$product_id && $product_id=$this->id;
if(!$product_id) return false;
$paramArr=array(
'fields'=>'cid,seller_cids,props,input_pids,input_str,pic_url
林戈的IT生涯
- 粉丝: 1w+
- 资源: 111
最新资源
- Android的在线云音乐播放器项目源码+文档说明(高分项目)
- 一个Java语言实现的简单版数据库 .zip
- springboot之资源库基础.pdf
- 基于java+spring+springMVC的学生考勤管理系统任务书.docx
- 一个Go语言编写的简单聊天室(终端形式).zip
- 基于java+spring+springMVCl的学生就业管理系统开题报告.doc
- 一个C++实现的简易动态语言解释器,可定义变量和函数,有if和while两种控制流语句,词法分析和语法分析分别使用flex和bison实现,参考自《flex & bison》.zip
- 深入理解编程中的回调函数:原理、实现及应用场景
- yolov8l-cls.pt
- 操作系统中银行家算法详解与Python实现防止死锁
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈