validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } try { if (strpos($this->url, 'item.jd')) { $id = $this->str_substr('item.jd.com/', '.html', $this->url); $id = $this->pregSubstr('/item.jd.[a-z]+\//', '/.html/', $this->url); if (empty($id[0])) { return [ 'code' => 1, 'msg' => '链接错误,请检查链接' ]; } return $this->j_copy($id[0]); } //淘宝连接检查URL的合法性 $arr = explode('?', $this->url); if (count($arr) < 2) { return [ 'code' => 1, 'msg' => '链接错误,请检查链接' ]; } if (strpos($arr[0], 'taobao')) { $id = $this->pregSubstr('/(\?id=|&id=)/', '/&/', $this->url); if (empty($id[0])) { return [ 'code' => 1, 'msg' => '链接错误,请检查链接' ]; } $data = $this->taobao($id[0]); return $data; } if (strpos($arr[0], 'tmall')) { $id = $this->pregSubstr('/(\?id=|&id=)/', '/&/', $this->url); if (empty($id[0])) { return [ 'code' => 1, 'msg' => '链接错误,请检查链接' ]; } $data = $this->tmall($id[0]); return $data; } if (strpos($arr[0], '1688')) { $id = $this->pregSubstr('/(\/offer\/)/', '/\.html/', $this->url); if (empty($id[0])) { return [ 'code' => 1, 'msg' => '链接错误,请检查链接' ]; } return $this->t1688($id[0]); } return [ 'code' => 1, 'msg' => '链接错误,请检查链接' ]; } catch (\Exception $e) { \Yii::error([__METHOD__, $e->getTrace()]); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } private function taobao($id) { $res = http_get($this->taobaoApi . '&itemid=' . $id); \Yii::error([__METHOD__, $res, $id, $this->taobaoApi]); if ($res->getStatusCode() != 200) { return [ 'code' => 1, 'msg' => '请求出错', ]; } $res = json_decode((string)$res->getBody(), true); \Yii::error([__METHOD__, $res, $id, $this->taobaoApi]); if (!empty($res['message']) || $res['retcode'] != '0000') { return [ 'code' => 1, 'msg' => !empty($res['message']) ? $res['message'] : $res['data'], ]; } // 售价 $price = null; if (!empty($res['data']['item']['priceRange'])) { $priceEx = explode('-', $res['data']['item']['priceRange']); $price = $priceEx[0]; } // 市场价 $marketPrice = null; if (!empty($res['data']['item']['marketPriceRange'])) { $marketrPriceEx = explode('-', $res['data']['item']['marketPriceRange']); $marketPrice = $marketrPriceEx[0]; } // 总库存 $quantity = 0; if (!empty($res['data']['item']['sku'][0])) { $quantity = $res['data']['item']['sku'][0]['quantity']; } // 详情 $desc = null; if (!empty($res['data']['item']['desc'])) { $desc = $res['data']['item']['desc']; } else { $desc = '

'; if (!empty($res['data']['item']['descImgs'])) { foreach ($res['data']['item']['descImgs'] as $key => $value) { $desc .= ''; } } $desc .= '

'; } // 视频 $video = null; if (!empty($res['data']['item']['videos'])) { $video = $res['data']['item']['videos'][0]['url']; } //虚拟销量 $vagueSellCount = 0; if (!empty($res['data']['item']['vagueSellCount'])) { $vagueSellCount = (int)$res['data']['item']['vagueSellCount']; } $images = $res['data']['item']['images']; foreach ($images as &$item) { $index = strstr($item, 'http'); if (!$index) { $item = 'https://' . $item; } $item = \str_replace('http://', 'https://', $item); $item = \str_replace('https:////', 'https://', $item); } $data = [ 'title' => $res['data']['item']['title'], 'pics' => $images, 'price' => $price, 'marketPrice' => $marketPrice, 'vagueSellCount' => $vagueSellCount, 'quantity' => $quantity, 'desc' => $desc, 'video' => $video, 'attr' => $this->exeAttr($res['data']['item']['sku']), 'attrGroup' => $this->exeAttrGroup($res['data']['item']['props']), ]; return [ 'code' => 0, 'msg' => 'success', 'data' => $data, ]; } private function tmall($id) { $res = http_get($this->tmallApi . '&itemid=' . $id); if ($res->getStatusCode() != 200) { return [ 'code' => 1, 'msg' => '请求出错', ]; } $res = json_decode((string)$res->getBody(), true); if (!empty($res['message']) || $res['retcode'] != '0000') { return [ 'code' => 1, 'msg' => !empty($res['message']) ? $res['message'] : $res['data'], ]; } // 售价 $price = null; if (!empty($res['data']['item']['priceRange'])) { $priceEx = explode('-', $res['data']['item']['priceRange']); $price = $priceEx[0]; } // 市场价 $marketPrice = null; if (!empty($res['data']['item']['marketPriceRange'])) { $marketrPriceEx = explode('-', $res['data']['item']['marketPriceRange']); $marketPrice = $marketrPriceEx[0]; } // 总库存 $quantity = 0; if (!empty($res['data']['item']['sku'][0])) { $quantity = $res['data']['item']['sku'][0]['quantity']; } // 详情 $desc = null; if (!empty($res['data']['item']['desc'])) { $desc = $res['data']['item']['desc']; } else { $desc = '

'; if ($res['data']['item']['descImgs']) { foreach ($res['data']['item']['descImgs'] as $key => $value) { $desc .= ''; } } $desc .= '

'; } // 视频 $video = null; if (!empty($res['data']['item']['videos'])) { $video = $res['data']['item']['videos'][0]['url']; } //虚拟销量 $vagueSellCount = 0; if (!empty($res['data']['item']['vagueSellCount'])) { $vagueSellCount = (int)$res['data']['item']['vagueSellCount']; } $images = $res['data']['item']['images']; foreach ($images as &$item) { $index = strstr($item, 'http'); if (!$index) { $item = 'https://' . $item; } $item = \str_replace('http://', 'https://', $item); $item = \str_replace('https:////', 'https://', $item); } $data = [ 'title' => $res['data']['item']['title'], 'pics' => $images, 'price' => $price, 'marketPrice' => $marketPrice, 'vagueSellCount' => $vagueSellCount, 'quantity' => $quantity, 'desc' => $desc, 'video' => $video, 'attr' => $this->exeAttr($res['data']['item']['sku']), 'attrGroup' => $this->exeAttrGroup($res['data']['item']['props']), ]; return [ 'code' => 0, 'msg' => 'success', 'data' => $data, ]; } private function t1688($id) { $res = http_get($this->t1688Api . '&num_iid=' . $id); if ($res->getStatusCode() != 200) { return [ 'code' => 1, 'msg' => '请求出错', ]; } $res = json_decode((string)$res->getBody(), true); if (!empty($res['message']) || $res['error_code'] != '0000') { return [ 'code' => 1, 'msg' => !empty($res['message']) ? $res['message'] : $res['data'], ]; } $title = $res['item']['title']; $price = $res['item']['price']; $marketPrice = $res['item']['orginal_price']; $quantity = $res['item']['num']; // 详情 if (!empty($res['item']['desc'])) { $desc = $res['item']['desc']; } else { $desc = '

'; foreach ($res['item']['item_imgs'] as $key => $value) { $desc .= ''; } $desc .= '

'; } // 视频 $video = null; if (!empty($res['item']['video'])) { $video = $res['item']['video'][0]['url'] ?? $res['item']['video']; } //虚拟销量 $vagueSellCount = 0; // if (!empty($res['data']['item']['vagueSellCount'])) { // $vagueSellCount = (int)$res['data']['item']['vagueSellCount']; // } $images = array_map(function($item){ $index = strstr($item['url'], 'http'); if (!$index) { $item['url'] = 'https://' . $item['url']; } $url = \str_replace('http://', 'https://', $item['url']); //兼容本来有两个斜杠问题 return \str_replace('https:////', 'https://', $url); }, $res['item']['item_imgs']); //兼容商城规格组数据格式 //兼容商品规格 $prop_imgs_arr = array(); if ($res['item']['prop_imgs']['prop_img']) { foreach ($res['item']['prop_imgs']['prop_img'] as $prop_img) { $prop_imgs_arr[$prop_img['propPath']] = $prop_img['url']; } } //兼容商城规格组数据格式 $attr_group_list_array = array(); if ($res['item']['props_list']) { foreach ($res['item']['props_list'] as $index => $item) { $prop_group = explode(':', $index); $prop_item = explode(':', $item); $attr_group_list_array[$prop_group[0]]['attr_group_name'] = $prop_item[0]; $attr_group_list_array[$prop_group[0]]['attr_list'][$prop_group[1]]['attr_name'] = $prop_item[1]; } } //兼容商城规格数据格式 $attr_list_array = array(); if ($res['item']['skus']['sku']) { foreach ($res['item']['skus']['sku'] as $index => $item) { $attr = [ 'num' => $item['quantity'], 'price' => $item['price'], 'cost_price' => $item['price'], 'no' => '', 'pic' => $res['item']['prop_imgs']['prop_img'][$index]['url'] ?: $res['item']['pic_url'], 'share_commission_first' => '', 'wholesale_price' => $item['price'], 'original_price' => $item['price'], 'weight' => 0, 'share_commission_second' => '', 'share_commission_third' => '', ]; $properties = explode(';', $item['properties_name']); $attrs = []; foreach ($properties as $properties_item) { $properties_item = explode(":", $properties_item); unset($properties_item[0], $properties_item[1]); $properties_item = array_values($properties_item); $attrs[] = [ 'attr_name' => $properties_item[1], 'attr_group_name' => $properties_item[0], ]; } $attr['attr_list'] = $attrs; $attr_list_array[] = $attr; } } $data = [ 'title' => $title, 'pics' => $images, 'price' => $price, 'marketPrice' => $marketPrice, 'vagueSellCount' => $vagueSellCount, 'quantity' => $quantity, 'desc' => $desc, 'video' => $video, 'attr' => $attr_list_array, 'attrGroup' => $attr_group_list_array, ]; return [ 'code' => 0, 'msg' => 'success', 'data' => $data, ]; } /** * 处理规格 */ protected function exeAttr($sku) { unset($sku[0]); $data = []; foreach ($sku as $key => $value) { $attr = [ 'num' => $value['quantity'], 'price' => $value['price'], 'cost_price' => $value['price'], 'wholesale_price' => $value['price'], 'original_price' => $value['price'], 'weight' => 0, 'no' => '', 'pic' => isset($value['image']) && !empty($value['image']) ? $value['image'] : '', 'share_commission_first' => '', 'share_commission_second' => '', 'share_commission_third' => '', ]; $attrEx = explode(';', $value['skuName']); $attrs = []; foreach ($attrEx as $k => $v) { $attrItemEx = explode('--', $v); $attrs[] = [ 'attr_name' => $attrItemEx[1], 'attr_group_name' => $attrItemEx[0], ]; } $attr['attr_list'] = $attrs; $data[] = $attr; } return $data; } // 处理规格组 protected function exeAttrGroup($props) { $data = []; if (is_array($props) && !empty($props)) { foreach ($props as $key => $value) { $group = [ 'attr_group_name' => $value['name'], ]; $groupValues = []; foreach ($value['values'] as $k => $v) { $groupValues[] = [ 'attr_name' => $v['name'], ]; } $group['attr_list'] = $groupValues; $data[] = $group; } } return $data; } public function copy1() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if (strpos($this->url, 'item.jd')) { $id = $this->preg_substr('/item.jd.[a-z]+\//', '/.html/', $this->url); $data = $this->j_copy($id[0]); if ($data['code'] == 1) { return $data; } return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } //淘宝连接检查URL的合法性 $arr = explode('?', $this->url); if (count($arr) < 2) { return [ 'code' => 1, 'msg' => '链接错误,请检查链接' ]; } if (strpos($arr[0], 'taobao') || strpos($arr[0], 'tmall')) { $id = $this->preg_substr('/(\?id=|&id=)/', '/&/', $this->url); $data = $this->t_copy($id[0]); if ($data['code'] == 1) { return $data; } return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } return [ 'code' => 1, 'msg' => '链接错误,请检查链接' ]; } public function t_copy($num_iid){ //图文详情 //商品基本信息 $title = ''; $goods_price_sale = ''; $goods_price = ''; $sale_count = ''; $quantity = ''; $attr_group_list = []; $checked_attr_list = []; $picsPath = []; $detail_info = ""; try { // $detail_api = "http://api.onebound.cn/taobao/api_call.php?api_name=item_get&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326"; // $html = $this->getHTTPS($detail_api); // $detail_json = json_decode($html, true); // \Yii::error($detail_json); // // 转化商品详情的编码 // if ($detail_json['item']['desc']) { // $detail_info = $detail_json['item']['desc']; // } else { // // } //临时处理办法,兼容淘宝详情采集为空的问题 $detail_api = "https://api-gw.onebound.cn/taobao/item_get/?key=tel18538854912&&num_iid={$num_iid}&is_promotion=1&&lang=zh-CN&secret=20200326"; $html = $this->getHTTPS($detail_api); $detail_json = json_decode($html, true); $detail_info = $detail_json['item']['desc']; $coding = mb_detect_encoding($detail_info, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5', 'ISO-8859-1')); $detail_info = mb_convert_encoding($detail_info, 'utf-8', $coding); $title = $detail_json['item']['title']; $goods_price_sale = $detail_json['item']['price']; $goods_price = $detail_json['item']['orginal_price']; $sale_count = $detail_json['item']['sales']; $video = $detail_json['item']['video']; $pic_url = $detail_json['item']['pic_url']; $quantity = ""; //兼容商品图片的数组格式 $picsPath_array=array(); if (!empty($detail_json['item']['item_imgs'])) { foreach($detail_json['item']['item_imgs'] as $key => $value){ if (strpos($value['url'],'http') === false) { $value['url'] = 'http:'. trim($value['url']); } $picsPath_array[$key]=$value['url']; } } $picsPath = $picsPath_array; //兼容商品规格 $prop_imgs_arr = array(); if ($detail_json['item']['prop_imgs']) { $prop_imgs = json_decode(json_encode($detail_json['item']['prop_imgs']),true); if ($prop_imgs['prop_img']) { foreach($prop_imgs['prop_img'] as $prop_img){ $prop_imgs_arr[$prop_img['properties']] = $prop_img['url']; } } } $prop_array=array(); foreach ($detail_json['item']['props_list'] as $dk=>$val) { $dks = explode(':',$dk); $vals = explode(':',$val); $prop_img = !empty($prop_imgs_arr[$dks[0].':'.$dks[1]]) ? $prop_imgs_arr[$dks[0] . ':' . $dks[1]] : ''; $prop_array[$dks[0]][$dks[1]]=array( 'prop_key' => $dks[0], 'prop_val' => $dks[1], 'name' => $vals[0], 'value' => $vals[1], 'pic_url' => $prop_img, ); } $mypropids = array_keys($prop_array); $mypropids = implode('|', $mypropids); //兼容商城规格组数据格式 $attr_group_list_array = array(); if (!empty($prop_array)) { $i = 0; foreach ($prop_array as $key=>$value) { $name = current($value); $attr_group_list_array[$i] = [ 'attr_group_name' => $name['name'] ]; $j = 0; foreach ($value as $key2 => $value) { $attr_group_list_array[$i]['attr_list'][$j]['attr_name']=$value['value']; $j++; } $i++; } } $attr_group_list = $attr_group_list_array; //兼容货品\库存\价格数据格式 $sku = $detail_json['item']['skus']['sku']; $checked_attr_list = array(); foreach ($sku as $key1 => $value1) { $attr_list = explode(';',$value1['properties_name']); foreach ($attr_list as $key2=>$value2) { $value2_arr = explode(':',$value2); $attr_name = $value2_arr[3]; $attr_group_name = $value2_arr[2]; $checked_attr_list[$key1]['attr_list'][$key2] = array("attr_id"=>null,'attr_name' => $attr_name, 'attr_group_name' => $attr_group_name); } $checked_attr_list[$key1]['price'] = $value1['price']; $checked_attr_list[$key1]['num'] = $value1['quantity']; } } catch (\Exception $e) {} return [ 'title' => $title,//标题 'pic_url' => $pic_url, 'picsPath' => $picsPath,//缩略图 'price' => $goods_price,//原价 'sale_price' => $goods_price_sale,//售价 'sale_count' => $sale_count,//销量 'quantity' => $quantity,//库存 'detail_info' => $detail_info,//图文详情 'video' => $video,//图文详情 'attr_group_list' => Json::encode($attr_group_list), 'checked_attr_list' => Json::encode($checked_attr_list), ]; } private function j_copy_detail($num_iid, $sku_id) { $html = $this->getHTTPS("https://api-gw.onebound.cn/jd/item_get_desc?api_name=item_get_desc&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326&main_sku_id={$sku_id}"); $detail_json = json_decode($html, true); $detail_info = $detail_json['item']['desc']; return $detail_info; } /** * @param $id * @return array * 京东抓取 */ public function j_copy($num_iid) { //图文详情 //商品基本信息 $title = ''; $goods_price_sale = ''; $goods_price = ''; $sale_count = ''; $quantity = ''; $attr_group_list = []; $checked_attr_list = []; $picsPath = []; $detail_info = ""; try { $html = $this->getHTTPS("https://api-gw.onebound.cn/jd/item_get?api_name=item_get&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326"); //$html = http_get("https://api-gw.onebound.cn/jd/item_get?api_name=item_get&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326"); $detail_json = json_decode($html, true); //转化商品详情的编码 $detail_info = $detail_json['item']['desc']; if (empty($detail_info)) { $detail_info = $this->j_copy_detail($num_iid, $detail_json['item']['main_sku_id']); } // $coding = mb_detect_encoding($detail_info, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5', 'ISO-8859-1')); // $detail_info = mb_convert_encoding($detail_info, 'utf-8', $coding); $detail_info = str_replace('data-lazyload', 'src', $detail_info); $end_index = stripos($detail_info, ''); if ($end_index !== false) { $detail_style_info = substr($detail_info, 0, ($end_index + 8)); $arr = $this->setDetail($detail_style_info); $img_str = ''; $detail_info = substr($end_index, ($end_index + 8)); foreach ($arr as $item) { $img_str .= ''; } $detail_info = $img_str .$detail_info; } $title =$detail_json['item']['title']; $goods_price_sale = $detail_json['item']['price']; $goods_price = $detail_json['item']['orginal_price']; $sale_count = $detail_json['item']['sales']; $video=$detail_json['item']['video']; $quantity = ""; //兼容商品图片的数组格式 $picsPath_array=array(); if(!empty($detail_json['item']['item_imgs'])){ foreach($detail_json['item']['item_imgs'] as $key=>$value){ $index = strstr($value['url'], 'http'); if (!$index) { $value['url'] = 'https://' . $value['url']; } $picsPath_array[$key]= \str_replace('http://', 'https://', $value['url']); //兼容本来有两个斜杠问题 $picsPath_array[$key]= \str_replace('https:////', 'https://', $picsPath_array[$key]); } } $picsPath = $picsPath_array; //兼容商品规格 $prop_imgs_arr = array(); if($detail_json['item']['prop_imgs']){ $prop_imgs = json_decode(json_encode($detail_json['item']['prop_imgs']),true); if($prop_imgs['prop_img']){ foreach($prop_imgs['prop_img'] as $prop_img){ $prop_imgs_arr[$prop_img['properties']] = $prop_img['url']; } } } $prop_array=array(); foreach($detail_json['item']['props_list'] as $dk=>$val){ $dks=explode(':',$dk); $vals=explode(':',$val); $prop_img = !empty($prop_imgs_arr[$dks[0].':'.$dks[1]])?$prop_imgs_arr[$dks[0].':'.$dks[1]]:''; $prop_array[$dks[0]][$dks[1]]=array( 'prop_key'=>$dks[0], 'prop_val'=>$dks[1], 'name'=>$vals[0], 'value'=>$vals[1], 'pic_url'=>$prop_img, ); } $mypropids= array_keys($prop_array); $mypropids= implode('|',$mypropids); //兼容商城规格组数据格式 $attr_group_list_array=array(); if(!empty($prop_array)){ $i=0; foreach($prop_array as $key=>$value){ $name=current($value); $attr_group_list_array[$i] = [ 'attr_group_name' => $name['name'] ]; $j=0; foreach($value as $key2=>$value){ $attr_group_list_array[$i]['attr_list'][$j]['attr_name']=$value['value']; $j++; } $i++; } } $attr_group_list =$attr_group_list_array; //兼容货品\库存\价格数据格式 $sku = $detail_json['item']['skus']['sku']; $checked_attr_list=array(); $share_level = ShareLevel::find()->where(['store_id' => get_store_id(), 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON]) ->select('level id, name')->orderBy('level ASC')->asArray()->all(); $share_level = array_merge([[ 'id' => 0, 'name' => '默认等级' ]], $share_level); foreach($sku as $key1=>$value1){ $attr_list=explode(';',$value1['properties_name']); foreach($attr_list as $key2=>$value2){ $value2_arr=explode(':',$value2); $attr_name=$value2_arr[3]; $attr_group_name=$value2_arr[2]; $checked_attr_list[$key1]['attr_list'][$key2]=array("attr_id"=>null,'attr_name'=>$attr_name,'attr_group_name'=>$attr_group_name); } $checked_attr_list[$key1]['price']=$value1['price']; $checked_attr_list[$key1]['cost_price']=$value1['price']; $checked_attr_list[$key1]['num']=$value1['quantity']; $checked_attr_list[$key1]['wholesale_price'] = $value1['price']; $checked_attr_list[$key1]['original_price'] = $value1['price']; $checked_attr_list[$key1]['weight'] = 0; $checked_attr_list[$key1]['share_commission_new_first'] = null; $checked_attr_list[$key1]['share_commission_new_second'] = null; $checked_attr_list[$key1]['share_commission_new_third'] = null; foreach ($share_level as $share_level_index => $share_level_) { $checked_attr_list[$key1]['share_commission_new_first'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id']; $checked_attr_list[$key1]['share_commission_new_first'][$share_level_index]['value'] = 0; $checked_attr_list[$key1]['share_commission_new_second'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id']; $checked_attr_list[$key1]['share_commission_new_second'][$share_level_index]['value'] = 0; $checked_attr_list[$key1]['share_commission_new_third'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id']; $checked_attr_list[$key1]['share_commission_new_third'][$share_level_index]['value'] = 0; } //$checked_attr_list[$key1]['pic']=$value1['quantity']; } } catch (\Exception $e) {} return [ 'code' => 0, 'data' => [ 'title' => $title,//标题 'pics' => $picsPath,//缩略图 'price' => $goods_price,//原价 'sale_price' => $goods_price_sale,//售价 'sale_count' => $sale_count,//销量 'quantity' => $quantity, //库存 'desc' => $detail_info,//图文详情 'video' => $video,//图文详情 'attrGroup' => $attr_group_list, 'attr' => $checked_attr_list, ] ]; } public function setDetail($detail_info, $arr = [], $index = 0) { $start = stripos($detail_info, 'background-image:url('); $end = stripos($detail_info, '.jpg'); if ($end !== false && $start !== false) { $arr[$index] = substr($detail_info, ($start), (($end + 4) - $start)); $arr[$index] = substr($arr[$index], 21); if (!stripos($arr[$index], 'http') && !stripos($arr[$index], 'https')) { $arr[$index] = 'https:' . $arr[$index]; } $new_detail_info = substr($detail_info, ($end + 4)); if (stripos($new_detail_info, 'background-image:url(')) { ++$index; return $this->setDetail($new_detail_info, $arr, $index); } } return $arr; } /** * @param $list //规格列表 * @param $level //规格列表下标 * @param array $attr_list * @param string $n * @param array $new_list * @return array * 规格列表数组重组 */ public function get_attr_list($list, $level, $attr_list = array(), $n = '', $new_list = array()) { foreach ($list[$level]['values'] as $key => $item) { $attr_list_new = $attr_list; $n_n = $n; $a = []; $n_n .= $list[$level]['propId'] . ':' . $item['valueId']; $a['attr_group_name'] = $list[$level]['propName']; $a['attr_id'] = null; $a['attr_name'] = $item['name']; array_push($attr_list_new, $a); if ($level < count($list) - 1 && $level < $this->level) { $new_list = $this->get_attr_list($list, $level + 1, $attr_list_new, $n_n . ';', $new_list); } else { $new_list_new['attr_list'] = $attr_list_new; $new_list_new['dataId'] = $n_n; $new_list[] = $new_list_new; } } return $new_list; } /** * @param $list //规格列表 * @param $level //规格列表下标 * @param array $attr_list * @param string $n * @param array $new_list * @return array * 规格列表数组重组 for jd by tianxin100 */ public function get_attr_list_for_jd($list, $level, $attr_list = array(), $n = '', $new_list = array()) { foreach ($list[$level]['attr_list'] as $key => $item) { $attr_list_new = $attr_list; //$n_n = $n; $a = []; // $n_n .= $list[$level]['propId'] . ':' . $item['valueId']; $a['attr_group_name'] = $list[$level]['attr_group_name']; $a['attr_id'] = null; $a['attr_name'] = $item['attr_name']; array_push($attr_list_new, $a); if ($level < count($list) - 1 && $level < $this->level) { $new_list[] = $this->get_attr_list_for_jd($list, $level + 1, $attr_list_new, $new_list); } else { $new_list_new['attr_list'] = $attr_list_new; //$new_list_new['dataId'] = $n_n; $new_list[] = $new_list_new; } } return $new_list; } /** * @param $start * @param $end * @param $str * @return array * 正则截取函数 */ public function pregSubstr($start, $end, $str) // 正则截取函数 { try { $temp = preg_split($start, $str); } catch (\Exception $e) { var_dump($str); exit(); } $result = []; foreach ($temp as $index => $value) { if ($index == 0) { continue; } $content = preg_split($end, $value); array_push($result, $content[0]); } return $result; } /** * @param $start * @param $end * @param $str * @return array * 字符串截取函数 */ function str_substr($start, $end, $str) // 字符串截取函数 { $temp = explode($start, $str); try { $result = []; foreach ($temp as $index => $value) { if ($index == 0) { continue; } $content = explode($end, $value); array_push($result, $content[0]); } return $result; } catch (\Exception $e) { var_dump($temp); return []; } } /** * @param $url * @return mixed * curl访问https */ public function getHTTPS($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HEADER, false); if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) { curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); return $result; } /** * @param $url * @return mixed * curl访问http */ public function normal_curl($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); //错误提示 if (curl_exec($curl) === false) { die(curl_error($curl)); } // 检查是否有错误发生 if (curl_errno($curl)) { echo 'Curl error: ' . curl_error($curl); } curl_close($curl); return $data; } //判断数组是几维数组 public function arrayLevel($arr){ $al = array(0); function aL($arr,&$al,$level=0){ if(is_array($arr)){ $level++; $al[] = $level; foreach($arr as $v){ aL($v,$al,$level); } } } aL($arr,$al); return max($al); } }