当前位置: 旋风说(旋风PHPer分享网)> 技术文章> 正文
thinkphp 5的代码写法很容易就能改成其它
<?php /** * Created by PhpStorm. * User: lyq * Date: 2019/05/24 * Time: 13:48 */ namespace app\common\model; use think\Db; use think\Exception; class Copy extends Common { //实例化后 直接调用 copyUrl 即可 static public function copyUrl($url){ if(strpos($url, 'https://item.taobao.com')===0){ //淘宝 return self::taobao($url); }elseif(strpos($url, 'https://detail.tmall.com')===0){ //天猫 return self::hejiangTm($url); }elseif(strpos($url, 'https://item.jd.com')===0){ //京东 return self::jd($url); }else{ throw new Exception('不支持此链接'); } } /** * 禾匠一个微擎应用团队的api * @param $url * @return array */ static public function hejiangTm($url){ $id = self::preg_substr('/(\?id=|&id=)/', '/&/', $url); $id = $id[0]; $api = "https://auth.zjhejiang.com/mall/copy/index?vid=".$id; $res = file_get_contents($api); $res = json_decode($res, true); if ($res && isset($res['code'])) { if ($res['code'] != 0) { return self::tm($url); } $html = $res['data']; $coding = mb_detect_encoding($html, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5', 'ISO-8859-1')); $html = mb_convert_encoding($html, 'UTF-8', $coding); $html = json_decode($html, true); return self::hejiangData($html,$url); } else { return self::tm($url); } } /** * 数据处理 * @param null $html * @param $url * @return array */ static private function hejiangData($html = null,$url) { $content_arr = $html; if ($content_arr['ret'][0] != 'SUCCESS::调用成功') { return self::tm($url); } $info = $content_arr['data']['itemInfoModel']; //标题 $title = $info['title']; //缩略图 $picsPath = $info['picsPath']; $pro_detail = json_decode($content_arr['data']['apiStack']['0']['value'], true); $pro_data = $pro_detail['data']; $pro_detail_info = $pro_data['itemInfoModel']; //商品价格 $price = isset($pro_detail_info['priceUnits'][1]) ? $pro_detail_info['priceUnits'][1]['price'] : $pro_detail_info['priceUnits'][0]['price']; $price_arr = explode('-', $price); $goods_price = $price_arr[0];//原价 $sale_price = $pro_detail_info['priceUnits'][0]['price']; $sale_price_arr = explode('-', $sale_price); $goods_price_sale = $sale_price_arr[0];//售价 //图文详情 $detail_info = isset($content_arr['data']['descInfo']) ? $content_arr['data']['descInfo'] : ""; return [ 'title' => $title,//标题 'thumb'=>$picsPath[0], 'images' => $picsPath,//组图 'price' => $goods_price_sale,//售价 'desc' => $detail_info//图文详情 ]; } /** * @param $start * @param $end * @param $str * @return array * 正则截取函数 */ static public function preg_substr($start, $end, $str){ $temp = preg_split($start, $str); $result = []; foreach ($temp as $index => $value) { if ($index == 0) { continue; } $content = preg_split($end, $value); array_push($result, $content[0]); } return $result; } /** * 淘宝 * @param $url * @return array * @throws Exception */ static public function taobao($url){ $text = file_get_contents($url); if(!$text){ throw new Exception('未抓取到内容,请检查链接是否支持'); } //商品名称 preg_match('/<title>([^<>]*)<\/title>/', $text, $title); $title = str_replace('-淘宝网','',iconv("GBK","UTF-8",$title[1])); $title = trim($title); //缩略图 preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $text, $img); $thumb = 'http:'.$img[1]; //价格 preg_match('/<em class="tb-rmb-num">([^<>]*)<\/em>/', $text, $price); $price = explode('-',$price[1]); $price = $price[0]; //组图 $needle = "auctionImages :"; $images = strstr($text, $needle); $images = strstr($images, "},",true); $images = str_replace($needle,'',$images); $images = explode(',',$images); foreach ($images as $key=>$val){ $val = str_replace(" ",'',$val); $val = str_replace("[",'',$val); $val = str_replace("]",'',$val); $val = str_replace('"','',$val); $images[$key] = 'http:'.$val; } //详情 $needle = "descUrl : location.protocol==='http:' ? '"; $descUrl = strstr($text, $needle); $descUrl = strstr($descUrl, "' : ",true); $descUrl = str_replace($needle,'',$descUrl); $desc = file_get_contents('http:'.$descUrl); $desc = iconv("gbk","UTF-8",$desc); $desc = str_replace("var desc='",'',$desc); $desc = str_replace("https:",'http:',$desc); $desc = str_replace("';",'',$desc); $desc = str_replace('\\','',$desc); $desc = preg_replace('/<a .*?href="(.*?)".*?>/is',"<a href='javascript:void(0)'>",$desc); return [ 'title'=>$title, 'thumb'=>$thumb, 'price'=>$price, 'images'=>$images, 'desc'=>$desc ]; } /** * 天猫 * @param $url * @return array * @throws Exception */ static public function tm($url){ $text = file_get_contents($url); if(!$text){ throw new Exception('未抓取到内容,请检查链接是否支持'); } //商品名称 preg_match('/<title>([^<>]*)<\/title>/', $text, $title); $title = str_replace('-tmall.com天猫','',iconv("GBK","UTF-8",$title[1])); $title = trim($title); //缩略图 preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $text, $img); $thumb = 'http:'.$img[1]; //价格 // preg_match('/<em class="tb-rmb-num">([^<>]*)<\/em>/', $text, $price); // $price = explode('-',$price[1]); // $price = $price[0]; $price = 0; //组图 preg_match('|<ul id="J_UlThumb" class="tb-thumb tm-clear">(.*)</ul>|isU',$text, $match); preg_match_all('/<img src="(.*?)" \//',$match[1],$images); $images = $images[1]; foreach ($images as $key=>$val){ $val = str_replace('_60x60q90.jpg','',$val); $images[$key]='http:'.$val; } //详情 preg_match_all('/TShop.Setup[(].*[)]?/is', $text, $content);//页面js脚本 $res = $content[0][0]; $pattern = '/\{.*\}/'; preg_match_all($pattern,$res,$mat); $ap = $mat[0][0]; $pattern1 = '/dsc.taobaocdn.com\/.*fetchDcUrl/'; preg_match($pattern1,$ap,$mat1); $mat2 = $mat1[0]; $mat3 = substr($mat2,0,-13); $url = "http://".$mat3; $goods_detail = file_get_contents($url); $desc = str_replace("var desc='",'',$goods_detail); $desc = str_replace("https:",'http:',$desc); $desc = str_replace("';",'',$desc); $desc = preg_replace('/<a .*?href="(.*?)".*?>/is',"<a href='javascript:void(0)'>",$desc); return [ 'title'=>$title, 'thumb'=>$thumb, 'price'=>$price, 'images'=>$images, 'desc'=>$desc ]; } /** * 抓取京东 * @param $url * @return array * @throws Exception */ static public function jd($url){ $text = file_get_contents($url); if(!$text){ throw new Exception('未抓取到内容,请检查链接是否支持'); } //商品名称 $needle = '<div class="sku-name">'; $title = strstr($text, $needle); $title = str_replace($needle,'',$title); $title = strstr(iconv('gbk','UTF-8',$title), "</div>",true); $title = trim($title); //缩略图 $needle = 'data-origin="'; $thumb = strstr($text, $needle); $thumb = str_replace($needle,'',$thumb); $thumb = strstr($thumb, '"',true); $thumb = 'http:'.$thumb; //价格 preg_match("/\/(\d+)*\./i", $url, $match_id); if(isset($match_id[1]) && $match_id[1]){ $id = $match_id[1]; //获取价格 $price_url = "http://p.3.cn/prices/mgets?skuIds=J_$id,J_&type=1"; $price_content = file_get_contents($price_url); if(!empty($price_content)){ $price_content_arr = json_decode($price_content,true); $price = $price_content_arr[0]['p']; }else{ $price = 0; } }else{ throw new Exception('京东商品id获取失败'); } //组图 $imgHead = explode('_',$thumb); $imgHead = $imgHead[0]; $needle = "imageList:"; $images = strstr($text, $needle); $images = strstr($images, "],",true); $images = str_replace($needle,'',$images); $images = explode(',',$images); foreach ($images as $key=>$val){ $val = str_replace(" ",'',$val); $val = str_replace("[",'',$val); $val = str_replace("]",'',$val); $val = str_replace('"','',$val); $images[$key] = $imgHead.'_'.$val; } //详情 $desc = ''; return [ 'title'=>$title, 'thumb'=>$thumb, 'price'=>$price, 'images'=>$images, 'desc'=>$desc ]; } }
当您发现内容错误或代码bug,以及下载链接无法使用等,请点击屏幕右下角的上报错误来进行提交,我们会尽快修正。
本程序所有源码和工具完全免费,当本网站内容如果侵犯了您的权益,请联系我们,我们会尽快处理,感谢您的合作。
相关文章
评论:
文明上网理性发言,请遵守 新闻评论服务协议