open淘宝酒店API open淘宝属性API open淘宝类目API
许天逸
2023-12-01
首先,在类目属性API下面我找到一个接口:taobao.itemcats.get
通过这个API我们可以获取所有的商品种类,我们就从这个地方查找酒店的种类ID(cid)
代码如下:
public function getAll()
{
//初始化淘宝API,将一些配置参数引入进来
$Taoapi_Config = Taoapi_Config::Init();
//实例化淘宝API
$Taoapi = new Taoapi;
//获取酒店cid
$Taoapi->method = 'taobao.itemcats.get';
$Taoapi->fields = 'cid,name';
//$Taoapi->parent_cid = 0; //注意:如果传了parent_cid或者cids,则忽略datetime
$Taoapi->datetime = '2005-01-01 00:00:00';
$ret = $Taoapi->Send('get','xml')->getArrayData();
return $ret;
}
我们发现酒店的cid就是50016161
然后我们先获取酒店都有哪些属性:
代码如下:
public function getpid()
{
//初始化淘宝API,将一些配置参数引入进来
$Taoapi_Config = Taoapi_Config::Init();
//实例化淘宝API
$Taoapi = new Taoapi;
//获取酒店cid
$Taoapi->method = 'taobao.itemprops.get';
$Taoapi->fields = 'pid,name';
$Taoapi->cid = '50016161';
$Taoapi->datetime = '2005-01-01 00:00:00';
$ret = $Taoapi->Send('get','xml')->getArrayData();
return $ret;
}
最后我们根据这些基本信息取回所有的酒店信息:
public function SearchHotels($city, $keyword, $page_size=5, $page_no=1, $order_by='volume', $price = 0, $star = 0)
{
//初始化淘宝API,将一些配置参数引入进来
$Taoapi_Config = Taoapi_Config::Init();
//实例化淘宝API
$Taoapi = new Taoapi;
//获取商品属性值 Get property values
$pvs = $this->GetPvs($Taoapi);
foreach($pvs['prop_values']['prop_value'] as $prop)
{
if(@strstr($prop['name'], $city) or @strstr($prop['name_alias'], $city)){
$Taoapi->props = $prop['pid'].':'.$prop['vid'].';';
break;
}
}
switch($price)
{
case 1:
$Taoapi->props .= '4209241:42425;';
break;
case 2:
$Taoapi->props .= '4209241:29820075;';
break;
case 3:
$Taoapi->props .= '4209241:63771061;';
break;
case 4:
$Taoapi->props .= '4209241:63771134;';
break;
case 5:
$Taoapi->props .= '4209241:45690;';
break;
default:
break;
}
switch($star)
{
case 1:
$Taoapi->props .= '4618707:63595280;';
break;
case 2:
$Taoapi->props .= '4618707:63595341;';
break;
case 3:
$Taoapi->props .= '4618707:105345;';
break;
case 4:
$Taoapi->props .= '4618707:72109529;';
break;
case 5:
$Taoapi->props .= '4618707:72109612;';
break;
case 6:
$Taoapi->props .= '4618707:72109692;';
break;
case 7:
$Taoapi->props .= '4618707:86482201;';
break;
case 8:
$Taoapi->props .= '4618707:86482260;';
break;
case 9:
$Taoapi->props .= '4618707:86482296;';
break;
case 10:
$Taoapi->props .= '4618707:86482353;';
break;
default:
break;
}
if(!empty($Taoapi->props)){
$Taoapi->props = trim($Taoapi->props,';');
}
$Taoapi->method = 'taobao.items.get';
$Taoapi->q = $keyword;
//$Taoapi->fields = 'detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual';
$Taoapi->fields = 'iid,num_iid,title,nick,pic_url,price,props_name,item_img,volume,score,detail_url';
$Taoapi->cid = '50016161';
if($order_by=='volume'){
$order_by .= ':desc';
}else if($order_by=='price'){
$order_by .= ':asc';
}else{
$order_by = 'volume:desc';
}
$Taoapi->order_by = $order_by;
$Taoapi->page_no = $page_no;
$Taoapi->page_size = $page_size;
$result = $Taoapi->Send('get','xml')->getArrayData();
return $result;
}
//这个是我们调用的获取pid,vid,name,name_alias的函数,可以帮我们找到具体的省市的酒店
public function GetPvs($Taoapi)
{
$Taoapi->method = 'taobao.itempropvalues.get';
$Taoapi->fields = 'pid,vid,name,name_alias';
$Taoapi->cid = '50016161';
$Taoapi->datetime = '2005-01-01 00:00:00';
$TaobaokeData = $Taoapi->Send('get','xml')->getArrayData();
return $TaobaokeData;
}