CopyTaobao.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. <?php
  2. /**
  3. * Project: 快速复制 淘宝、天猫、1688、京东 商品到CRMEB系统
  4. * Author: 有一片天 <810806442@qq.com> 微信:szktor
  5. * Date: 2019-04-25
  6. */
  7. namespace app\admin\controller\store;
  8. use service\HttpService;
  9. use service\UploadService;
  10. use think\exception\PDOException;
  11. use app\admin\controller\AuthController;
  12. use app\admin\model\system\SystemConfig;
  13. use traits\CurdControllerTrait;
  14. use service\JsonService;
  15. use service\UtilService;
  16. use app\admin\model\store\StoreCategory as CategoryModel;
  17. use app\admin\model\store\StoreProduct as ProductModel;
  18. use app\admin\model\system\SystemAttachment;
  19. use app\admin\model\system\SystemAttachmentCategory;
  20. /**
  21. * 产品管理
  22. * Class StoreProduct
  23. * @package app\admin\controller\store
  24. */
  25. class CopyTaobao extends AuthController
  26. {
  27. use CurdControllerTrait;
  28. protected $bindModel = ProductModel::class;
  29. //错误信息
  30. protected $errorInfo=true;
  31. //产品默认字段
  32. protected $productInfo=[
  33. 'cate_id'=>'',
  34. 'store_name' =>'',
  35. 'store_info' => '',
  36. 'unit_name' => '件',
  37. 'price' => 0,
  38. 'keyword' => '',
  39. 'ficti' => 0,
  40. 'ot_price' => 0,
  41. 'give_integral' => 0,
  42. 'postage' => 0,
  43. 'cost' => 0,
  44. 'image' => '',
  45. 'slider_image' => '',
  46. 'add_time' => 0,
  47. 'stock' => 0,
  48. 'description' => '',
  49. 'soure_link' => ''
  50. ];
  51. //抓取网站主域名
  52. protected $grabName=[
  53. 'taobao',
  54. '1688',
  55. 'tmall',
  56. 'jd'
  57. ];
  58. //远程下载附件图片分类名称
  59. protected $AttachmentCategoryName='远程下载';
  60. /**
  61. * 显示资源
  62. * @return html
  63. */
  64. public function index()
  65. {
  66. $list = CategoryModel::getTierList();
  67. $menus=[];
  68. foreach ($list as $menu){
  69. $menus[] = ['value'=>$menu['id'],'label'=>$menu['html'].$menu['cate_name'],'disabled'=>$menu['pid']== 0];//,'disabled'=>$menu['pid']== 0];
  70. }
  71. $this->assign('menus',$menus);
  72. $this->assign('is_layui',1);
  73. return $this->fetch();
  74. }
  75. /*
  76. * 设置错误信息
  77. * @param string $msg 错误信息
  78. * */
  79. public function setErrorInfo($msg='')
  80. {
  81. $this->errorInfo=$msg;
  82. return false;
  83. }
  84. /*
  85. * 设置字符串字符集
  86. * @param string $str 需要设置字符集的字符串
  87. * @return string
  88. * */
  89. public function Utf8String($str)
  90. {
  91. $encode=mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
  92. if(strtoupper($encode) != 'UTF-8') $str=mb_convert_encoding($str, 'utf-8',$encode);
  93. return $str;
  94. }
  95. /**
  96. * 获取资源,并解析出对应的商品参数
  97. * @return json
  98. */
  99. public function get_request_contents()
  100. {
  101. list($link)=UtilService::postMore([
  102. ['link','']
  103. ],$this->request,true);
  104. $url=$this->checkurl($link);
  105. if($url===false) return JsonService::fail($this->errorInfo);
  106. $this->errorInfo=true;
  107. $html=$this->curl_Get($url,60);
  108. if(!$html) return JsonService::fail('商品HTML信息获取失败');
  109. $html=$this->Utf8String($html);
  110. preg_match('/<title>([^<>]*)<\/title>/', $html, $title);
  111. //商品标题
  112. $this->productInfo['store_name'] = isset($title['1']) ? str_replace(['-淘宝网','-tmall.com天猫',' - 阿里巴巴',' ','-','【图片价格品牌报价】京东','京东','【行情报价价格评测】'],'',trim($title['1'])) :'';
  113. $this->productInfo['store_info'] = $this->productInfo['store_name'];
  114. try{
  115. //获取url信息
  116. $pathinfo=pathinfo($url);
  117. if(!isset($pathinfo['dirname'])) return JsonService::fail('解析URL失败');
  118. //提取域名
  119. $parse_url=parse_url($pathinfo['dirname']);
  120. if(!isset($parse_url['host'])) return JsonService::fail('获取域名失败');
  121. //获取第一次.出现的位置
  122. $strLeng=strpos($parse_url['host'],'.')+1;
  123. //截取域名中的真实域名不带.com后的
  124. $funsuffix=substr($parse_url['host'],$strLeng,strrpos($parse_url['host'],'.')-$strLeng);
  125. if(!in_array($funsuffix,$this->grabName)) return JsonService::fail('您输入的地址不在复制范围内!');
  126. //设拼接设置产品函数
  127. $funName="setProductInfo".ucfirst($funsuffix);
  128. //执行方法
  129. if(method_exists($this,$funName))
  130. $this->$funName($html);
  131. else
  132. return JsonService::fail('设置产品函数不存在');
  133. if(!$this->productInfo['slider_image']) return JsonService::fail('未能获取到商品信息,请确保商品信息有效!');
  134. return JsonService::successful($this->productInfo);
  135. }catch (\Exception $e){
  136. return JsonService::fail('系统错误',['line'=>$e->getLine(),'meass'=>$e->getMessage()]);
  137. }
  138. }
  139. /*
  140. * 淘宝设置产品
  141. * @param string $html 网页内容
  142. * */
  143. public function setProductInfoTaobao($html)
  144. {
  145. //获取轮播图
  146. $images = $this->getTaobaoImg($html);
  147. $images = array_merge($images);
  148. $this->productInfo['slider_image']=isset($images['gaoqing']) ? $images['gaoqing'] : (array)$images;
  149. //获取产品详情请求链接
  150. $link=$this->getTaobaoDesc($html);
  151. //获取请求内容
  152. $desc_json = HttpService::getRequest($link);
  153. //转换字符集
  154. $desc_json = $this->Utf8String($desc_json);
  155. //截取掉多余字符
  156. $this->productInfo['test']=$desc_json;
  157. $desc_json = str_replace('var desc=\'','',$desc_json);
  158. $desc_json = str_replace(["\n","\t","\r"],'',$desc_json);
  159. $content = substr($desc_json,0,-2);
  160. $this->productInfo['description']=$content;
  161. //获取详情图
  162. $description_images=$this->decodedesc($this->productInfo['description']);
  163. $this->productInfo['description_images']=is_array($description_images) ? $description_images : [];
  164. $this->productInfo['image']=is_array($this->productInfo['slider_image']) && isset($this->productInfo['slider_image'][0]) ? $this->productInfo['slider_image'][0] : '';
  165. }
  166. /*
  167. * 天猫设置产品
  168. * @param string $html 网页内容
  169. * */
  170. public function setProductInfoTmall($html)
  171. {
  172. //获取轮播图
  173. $images = $this->getTianMaoImg($html);
  174. $images = array_merge($images);
  175. $this->productInfo['slider_image'] = $images;
  176. $this->productInfo['image'] = is_array($this->productInfo['slider_image']) && isset($this->productInfo['slider_image'][0]) ? $this->productInfo['slider_image'][0] : '';
  177. //获取产品详情请求链接
  178. $link=$this->getTianMaoDesc($html);
  179. //获取请求内容
  180. $desc_json = HttpService::getRequest($link);
  181. //转换字符集
  182. $desc_json = $this->Utf8String($desc_json);
  183. //截取掉多余字符
  184. $desc_json = str_replace('var desc=\'','',$desc_json);
  185. $desc_json = str_replace(["\n","\t","\r"],'',$desc_json);
  186. $content = substr($desc_json,0,-2);
  187. $this->productInfo['description']=$content;
  188. //获取详情图
  189. $description_images=$this->decodedesc($this->productInfo['description']);
  190. $this->productInfo['description_images']=is_array($description_images) ? $description_images : [];
  191. }
  192. /*
  193. * 1688设置产品
  194. * @param string $html 网页内容
  195. * */
  196. public function setProductInfo1688($html)
  197. {
  198. //获取轮播图
  199. $images = $this->get1688Img($html);
  200. if(isset($images['gaoqing'])){
  201. $images['gaoqing'] = array_merge($images['gaoqing']);
  202. $this->productInfo['slider_image'] = $images['gaoqing'];
  203. }else
  204. $this->productInfo['slider_image'] = $images;
  205. $this->productInfo['image'] = is_array($this->productInfo['slider_image']) && isset($this->productInfo['slider_image'][0]) ? $this->productInfo['slider_image'][0] : '';
  206. //获取产品详情请求链接
  207. $link = $this->get1688Desc($html);
  208. //获取请求内容
  209. $desc_json = HttpService::getRequest($link);
  210. //转换字符集
  211. $desc_json = $this->Utf8String($desc_json);
  212. $this->productInfo['test']=$desc_json;
  213. //截取掉多余字符
  214. $desc_json = str_replace('var offer_details=','',$desc_json);
  215. $desc_json = str_replace(["\n","\t","\r"],'',$desc_json);
  216. $desc_json = substr($desc_json,0,-1);
  217. $descArray = json_decode($desc_json,true);
  218. if(!isset($descArray['content'])) $descArray['content'] = '';
  219. $this->productInfo['description']=$descArray['content'];
  220. //获取详情图
  221. $description_images=$this->decodedesc($this->productInfo['description']);
  222. $this->productInfo['description_images']=is_array($description_images) ? $description_images : [];
  223. }
  224. /*
  225. * JD设置产品
  226. * @param string $html 网页内容
  227. * */
  228. public function setProductInfoJd($html)
  229. {
  230. //获取产品详情请求链接
  231. $desc_url = $this->getJdDesc($html);
  232. //获取请求内容
  233. $desc_json=HttpService::getRequest($desc_url);
  234. //转换字符集
  235. $desc_json = $this->Utf8String($desc_json);
  236. //截取掉多余字符
  237. if(substr($desc_json,0,8) == 'showdesc') $desc_json = str_replace('showdesc','',$desc_json);
  238. $desc_json = str_replace('data-lazyload=','src=',$desc_json);
  239. $descArray=json_decode($desc_json,true);
  240. if(!$descArray) $descArray=['content'=>''];
  241. //获取轮播图
  242. $images=$this->getJdImg($html);
  243. $images = array_merge($images);
  244. $this->productInfo['slider_image']=$images;
  245. $this->productInfo['image']=is_array($this->productInfo['slider_image']) ? $this->productInfo['slider_image'][0] : '';
  246. $this->productInfo['description']=$descArray['content'];
  247. //获取详情图
  248. $description_images=$this->decodedesc($descArray['content']);
  249. $this->productInfo['description_images']=is_array($description_images) ? $description_images : [];
  250. }
  251. /*
  252. * 检查淘宝,天猫,1688的商品链接
  253. * @return string
  254. */
  255. public function checkurl($link)
  256. {
  257. $link=strtolower($link);
  258. if(!$link) return $this->setErrorInfo('请输入链接地址');
  259. if(substr($link,0,4)!='http') return $this->setErrorInfo('链接地址必须以http开头');
  260. $arrLine=explode('?',$link);
  261. if(!count($arrLine)) return $this->setErrorInfo('链接地址有误(ERR:1001)');
  262. if(!isset($arrLine[1])){
  263. if(strpos($link,'1688')!==false && strpos($link,'offer')!==false) return trim($arrLine[0]);
  264. else if(strpos($link,'item.jd')!==false) return trim($arrLine[0]);
  265. else return $this->setErrorInfo('链接地址有误(ERR:1002)');
  266. }
  267. if(strpos($link,'1688')!==false && strpos($link,'offer')!==false) return trim($arrLine[0]);
  268. if(strpos($link,'item.jd')!==false) return trim($arrLine[0]);
  269. $arrLineValue = explode('&',$arrLine[1]);
  270. if(!is_array($arrLineValue)) return $this->setErrorInfo('链接地址有误(ERR:1003)');
  271. if(!strpos(trim($arrLine[0]),'item.htm')) $this->setErrorInfo('链接地址有误(ERR:1004)');
  272. //链接参数
  273. $lastStr='';
  274. foreach ($arrLineValue as $k => $v){
  275. if(substr(strtolower($v),0,3) == 'id='){
  276. $lastStr = trim($v);
  277. break;
  278. }
  279. }
  280. if(!$lastStr) return $this->setErrorInfo('链接地址有误(ERR:1005)');
  281. return trim($arrLine[0]) . '?' . $lastStr;
  282. }
  283. /*
  284. * 保存图片保存产品信息
  285. * */
  286. public function save_product()
  287. {
  288. $data=UtilService::postMore([
  289. ['cate_id',''],
  290. ['store_name',''],
  291. ['store_info',''],
  292. ['keyword',''],
  293. ['unit_name',''],
  294. ['image',''],
  295. ['slider_image',[]],
  296. ['price',''],
  297. ['ot_price',''],
  298. ['give_integral',''],
  299. ['postage',''],
  300. ['sales',''],
  301. ['ficti',''],
  302. ['stock',''],
  303. ['cost',''],
  304. ['description_images',[]],
  305. ['description',''],
  306. ['is_show',0],
  307. ['soure_link',''],
  308. ]);
  309. if(!$data['cate_id']) return JsonService::fail('请选择分类!');
  310. if(!$data['store_name']) return JsonService::fail('请填写产品名称');
  311. if(!$data['unit_name']) return JsonService::fail('请填写产品单位');
  312. if(!$data['image']) return JsonService::fail('商品主图暂无,无法保存商品,您可选择其他链接进行复制产品');
  313. if($data['price'] == '' || $data['price'] < 0) return JsonService::fail('请输入产品售价');
  314. if($data['ot_price'] == '' || $data['ot_price'] < 0) return JsonService::fail('请输入产品市场价');
  315. if($data['stock'] == '' || $data['stock'] < 0) return JsonService::fail('请输入库存');
  316. //查询附件分类
  317. $AttachmentCategory = SystemAttachmentCategory::where(['name' =>$this->AttachmentCategoryName])->find();
  318. //不存在则创建
  319. if(!$AttachmentCategory) $AttachmentCategory = SystemAttachmentCategory::set(['pid' =>'0','name' =>$this->AttachmentCategoryName,'enname'=>'']);
  320. //生成附件目录
  321. if(makePathToUrl('attach',3) == '') return JsonService::fail('无法创建文件夹,请检查您的上传目录权限:'.UPLOAD_PATH.'/attach/');
  322. ini_set("max_execution_time", 600);
  323. //开始图片下载处理
  324. ProductModel::beginTrans();
  325. try{
  326. //放入主图
  327. $images=[
  328. ['w'=>305, 'h'=>305, 'line'=>$data['image'],'valuename'=>'image']
  329. ];
  330. //放入轮播图
  331. foreach ($data['slider_image'] as $item){
  332. $value=['w'=>640, 'h'=>640, 'line'=>$item,'valuename'=>'slider_image','isTwoArray'=>true];
  333. array_push($images,$value);
  334. }
  335. //执行下载
  336. $res=$this->uploadImage($images,false,0,$AttachmentCategory['id']);
  337. if(!is_array($res)) return JsonService::fail($this->errorInfo ? $this->errorInfo : '保存图片失败');
  338. if(isset($res['image'])) $data['image']=$res['image'];
  339. if(isset($res['slider_image'])) $data['slider_image']=$res['slider_image'];
  340. $data['slider_image']=count($data['slider_image']) ? json_encode($data['slider_image']) : '';
  341. //替换并下载详情里面的图片默认下载全部图片
  342. $data['description']=preg_replace('#<style>.*?</style>#is','',$data['description']);
  343. $data['description']=$this->uploadImage($data['description_images'],$data['description'],1,$AttachmentCategory['id']);
  344. unset($data['description_images']);
  345. $data['add_time']=time();
  346. $cate_id = explode(',',$data['cate_id']);
  347. //产品存在
  348. if($productInfo=ProductModel::where(['soure_link'=>$data['soure_link']])->find()){
  349. $productInfo->description=$data['description'];
  350. $productInfo->slider_image=$data['slider_image'];
  351. $productInfo->image=$data['image'];
  352. $productInfo->store_name=$data['store_name'];
  353. $productInfo->save();
  354. ProductModel::commitTrans();
  355. return JsonService::successful('商品存在,信息已被更新成功');
  356. }else {
  357. //不存在时新增
  358. if ($res = ProductModel::set($data)) {
  359. foreach ($cate_id as $cid) {
  360. \think\Db::name('store_product_cate')->insert(['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()]);
  361. }
  362. ProductModel::commitTrans();
  363. return JsonService::successful('生成产品成功');
  364. } else {
  365. ProductModel::rollbackTrans();
  366. return JsonService::fail('生成产品失败');
  367. }
  368. }
  369. }catch (PDOException $e){
  370. ProductModel::rollbackTrans();
  371. return JsonService::fail('插入数据库错误',['line'=>$e->getLine(),'messag'=>$e->getMessage()]);
  372. }catch (\Exception $e){
  373. ProductModel::rollbackTrans();
  374. return JsonService::fail('系统错误',['line'=>$e->getLine(),'messag'=>$e->getMessage()]);
  375. }
  376. }
  377. /*
  378. * 上传图片处理
  379. * @param array $image 图片路径
  380. * @param int $uploadType 上传方式 0=远程下载
  381. * */
  382. public function uploadImage(array $images=[],$html='',$uploadType=0,$AttachmentCategoryId=0)
  383. {
  384. $uploadImage=[];
  385. $siteUrl = SystemConfig::getValue('site_url');
  386. switch ($uploadType){
  387. case 0:
  388. foreach ($images as $item){
  389. //下载图片文件
  390. if($item['w'] && $item['h'])
  391. $uploadValue=$this->downloadImage($item['line'],'',0,30,$item['w'],$item['h']);
  392. else
  393. $uploadValue=$this->downloadImage($item['line']);
  394. //下载成功更新数据库
  395. if(is_array($uploadValue)){
  396. //TODO 拼接图片地址
  397. if($uploadValue['image_type'] == 1) $imagePath=$siteUrl.$uploadValue['path'];
  398. else $imagePath = $uploadValue['path'];
  399. //写入数据库
  400. if(!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'],$uploadValue['size'],$uploadValue['mime'],$imagePath,$imagePath,$AttachmentCategoryId,$uploadValue['image_type'],time(),1);
  401. //组装数组
  402. if(isset($item['isTwoArray']) && $item['isTwoArray'])
  403. $uploadImage[$item['valuename']][]=$imagePath;
  404. else
  405. $uploadImage[$item['valuename']]=$imagePath;
  406. }
  407. }
  408. break;
  409. case 1:
  410. preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $html, $match);
  411. if(isset($match[1])){
  412. foreach ($match[1] as $item){
  413. if(is_int(strpos($item, 'http')))
  414. $arcurl = $item;
  415. else
  416. $arcurl = 'http://'.ltrim($item,'\//');
  417. $uploadValue=$this->downloadImage($arcurl);
  418. //下载成功更新数据库
  419. if(is_array($uploadValue)){
  420. //TODO 拼接图片地址
  421. if($uploadValue['image_type'] == 1) $imagePath=$siteUrl.$uploadValue['path'];
  422. else $imagePath = $uploadValue['path'];
  423. //写入数据库
  424. if(!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'],$uploadValue['size'],$uploadValue['mime'],$imagePath,$imagePath,$AttachmentCategoryId,$uploadValue['image_type'],time(),1);
  425. //替换图片
  426. $html=str_replace($item,$imagePath,$html);
  427. }else{
  428. //替换掉没有下载下来的图片
  429. $html=preg_replace('#<img.*?src="'.$item.'"*>#i','',$html);
  430. }
  431. }
  432. }
  433. return $html;
  434. break;
  435. default:
  436. return $this->setErrorInfo('上传方式错误');
  437. break;
  438. }
  439. return $uploadImage;
  440. }
  441. //提取商品描述中的所有图片
  442. public function decodedesc($desc = '')
  443. {
  444. $desc =trim($desc);
  445. if(!$desc) return '';
  446. preg_match_all('/<img[^>]*?src="([^"]*?)"[^>]*?>/i',$desc,$match);
  447. if(!isset($match[1]) || count($match[1]) <= 0){
  448. preg_match_all('/:url(([^"]*?));/i',$desc,$match);
  449. if(!isset($match[1]) || count($match[1]) <= 0) return $desc;
  450. }else{
  451. preg_match_all('/:url(([^"]*?));/i',$desc,$newmatch);
  452. if(isset($newmatch[1]) && count($newmatch[1]) > 0) $match[1] = array_merge($match[1],$newmatch[1]);
  453. }
  454. $match[1] = array_unique($match[1]); //去掉重复
  455. foreach($match[1] as $k => &$v) {
  456. $_tmp_img = str_replace([')','(',';'],'',$v);
  457. $_tmp_img = strpos($_tmp_img,'http') ? $_tmp_img : 'http:' . $_tmp_img;
  458. if(strpos($v,'?')) {
  459. $_tarr = explode('?',$v);
  460. $_tmp_img = trim($_tarr[0]);
  461. }
  462. $_urls = str_replace(['\'','"'],'',$_tmp_img);
  463. if($this->_img_exists($_urls)) $v = $_urls;
  464. }
  465. return $match[1];
  466. }
  467. //获取京东商品组图
  468. public function getJdImg($html = '')
  469. {
  470. //获取图片服务器网址
  471. preg_match('/<img(.*?)id="spec-img"(.*?)data-origin=\"(.*?)\"[^>]*>/', $html, $img);
  472. if(!isset($img[3])) return '';
  473. $info = parse_url(trim($img[3]));
  474. if(!$info['host']) return '';
  475. if(!$info['path']) return '';
  476. $_tmparr = explode('/',trim($info['path']));
  477. $url = 'http://' . $info['host'] . '/' . $_tmparr[1] . '/' . str_replace(['jfs',' '],'',trim($_tmparr[2]));
  478. preg_match('/imageList:(.*?)"],/is', $html, $img);
  479. if(!isset($img[1]))
  480. {
  481. return '';
  482. }
  483. $_arr = explode(',',$img[1]);
  484. foreach ($_arr as $k => &$v){
  485. $_str = $url . str_replace(['"','[',']',' '],'',trim($v));
  486. if(strpos($_str,'?'))
  487. {
  488. $_tarr = explode('?',$_str);
  489. $_str = trim($_tarr[0]);
  490. }
  491. if($this->_img_exists($_str))
  492. {
  493. $v = $_str;
  494. }else{
  495. unset($_arr[$k]);
  496. }
  497. }
  498. return array_unique($_arr);
  499. }
  500. //获取京东商品描述
  501. public function getJdDesc($html = '')
  502. {
  503. preg_match('/,(.*?)desc:([^<>]*)\',/i', $html, $descarr);
  504. if(!isset($descarr[1]) && !isset($descarr[2])) return '';
  505. $tmpArr = explode(',',$descarr[2]);
  506. if(count($tmpArr) > 0)
  507. {
  508. $descarr[2] = trim($tmpArr[0]);
  509. }
  510. $replace_arr = ['\'','\',',' ',',','/*','*/'];
  511. if(isset($descarr[2]))
  512. {
  513. $d_url = str_replace($replace_arr,'',$descarr[2]);
  514. return $this->formatDescUrl(strpos($d_url,'http') ? $d_url : 'http:' . $d_url);
  515. }
  516. $d_url = str_replace($replace_arr,'',$descarr[1]);
  517. $d_url = $this->formatDescUrl($d_url);
  518. $d_url = rtrim(rtrim($d_url,"?"),"&");
  519. return substr($d_url,0,4) == 'http' ? $d_url : 'http:' . $d_url;
  520. }
  521. //处理下京东商品描述网址
  522. public function formatDescUrl($url = '')
  523. {
  524. if(!$url) return '';
  525. $url = substr($url,0,4) == 'http' ? $url : 'http:' . $url;
  526. if(!strpos($url,'&'))
  527. {
  528. $_arr = explode('?',$url);
  529. if(!is_array($_arr) || count($_arr) <= 0) return $url;
  530. return trim($_arr[0]);
  531. }else{
  532. $_arr = explode('&',$url);
  533. }
  534. if(!is_array($_arr) || count($_arr) <= 0) return $url;
  535. unset($_arr[count($_arr)-1]);
  536. $new_url = '';
  537. foreach ($_arr as $k => $v) {
  538. $new_url .= $v . '&';
  539. }
  540. return !$new_url ? $url : $new_url;
  541. }
  542. //获取1688商品组图
  543. public function get1688Img($html = '')
  544. {
  545. preg_match('/<ul class=\"nav nav-tabs fd-clr\">(.*?)<\/ul>/is', $html, $img);
  546. if(!isset($img[0]))
  547. {
  548. return '';
  549. }
  550. preg_match_all('/preview":"(.*?)\"\}\'>/is', $img[0], $arrb);
  551. if(!isset($arrb[1]) || count($arrb[1]) <= 0)
  552. {
  553. return '';
  554. }
  555. $thumb = [];
  556. $gaoqing = [];
  557. $res = ['thumb' => '', 'gaoqing' => '']; //缩略图片和高清图片
  558. foreach($arrb[1] as $k => $v)
  559. {
  560. $_str = str_replace(['","original":"'],'*',$v);
  561. $_arr = explode('*',$_str);
  562. if(is_array($_arr) && isset($_arr[0]) && isset($_arr[1]))
  563. {
  564. if(strpos($_arr[0],'?'))
  565. {
  566. $_tarr = explode('?',$_arr[0]);
  567. $_arr[0] = trim($_tarr[0]);
  568. }
  569. if(strpos($_arr[1],'?'))
  570. {
  571. $_tarr = explode('?',$_arr[1]);
  572. $_arr[1] = trim($_tarr[0]);
  573. }
  574. if($this->_img_exists($_arr[0])) $thumb[] = trim($_arr[0]);
  575. if($this->_img_exists($_arr[1])) $gaoqing[] = trim($_arr[1]);
  576. }
  577. }
  578. $res = ['thumb' => array_unique($thumb), 'gaoqing' => array_unique($gaoqing)]; //缩略图片和高清图片
  579. return $res;
  580. }
  581. //获取1688商品描述
  582. public function get1688Desc($html = '')
  583. {
  584. preg_match('/data-tfs-url="([^<>]*)data-enable="true"/', $html, $descarr);
  585. if(!isset($descarr[1])) return '';
  586. return str_replace(['"',' '],'',$descarr[1]);
  587. }
  588. //获取天猫商品组图
  589. public function getTianMaoImg($html = '')
  590. {
  591. $pic_size = '430';
  592. preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $html, $img);
  593. if(isset($img[1]))
  594. {
  595. $_arr = explode('x',$img[1]);
  596. $filename = $_arr[count($_arr)-1];
  597. $pic_size = intval(substr($filename,0,3));
  598. }
  599. preg_match('|<ul id="J_UlThumb" class="tb-thumb tm-clear">(.*)</ul>|isU',$html, $match);
  600. preg_match_all('/<img src="(.*?)" \//',$match[1],$images);
  601. if(!isset($images[1])) return '';
  602. foreach($images[1] as $k => &$v)
  603. {
  604. $tmp_v = trim($v);
  605. $_arr = explode('x',$tmp_v);
  606. $_fname = $_arr[count($_arr)-1];
  607. $_size = intval(substr($_fname,0,3));
  608. if(strpos($tmp_v,'://'))
  609. {
  610. $_arr = explode(':',$tmp_v);
  611. $r_url = trim($_arr[1]);
  612. }else{
  613. $r_url = $tmp_v;
  614. }
  615. $str = str_replace($_size, $pic_size, $r_url);
  616. if(strpos($str,'?'))
  617. {
  618. $_tarr = explode('?',$str);
  619. $str = trim($_tarr[0]);
  620. }
  621. $_i_url = strpos($str,'http') ? $str : 'http:' . $str;
  622. if($this->_img_exists($_i_url))
  623. {
  624. $v = $_i_url;
  625. }else{
  626. unset($images[1][$k]);
  627. }
  628. }
  629. return array_unique($images[1]);
  630. }
  631. //获取天猫商品描述
  632. public function getTianMaoDesc($html = '')
  633. {
  634. preg_match('/descUrl":"([^<>]*)","httpsDescUrl":"/', $html, $descarr);
  635. if(!isset($descarr[1]))
  636. {
  637. preg_match('/httpsDescUrl":"([^<>]*)","fetchDcUrl/', $html, $descarr);
  638. if(!isset($descarr[1])) return '';
  639. }
  640. return strpos($descarr[1],'http') ? $descarr[1] : 'http:' . $descarr[1];
  641. }
  642. //获取淘宝商品组图
  643. public function getTaobaoImg($html = '')
  644. {
  645. preg_match('/auctionImages([^<>]*)"]/', $html, $imgarr);
  646. if(!isset($imgarr[1])) return '';
  647. $arr = explode(',', $imgarr[1]);
  648. foreach($arr as $k => &$v)
  649. {
  650. $str = trim($v);
  651. $str = str_replace(['"',' ','',':['], '', $str);
  652. if(strpos($str,'?'))
  653. {
  654. $_tarr = explode('?',$str);
  655. $str = trim($_tarr[0]);
  656. }
  657. $_i_url = strpos($str,'http') ? $str : 'http:' . $str;
  658. if($this->_img_exists($_i_url))
  659. {
  660. $v = $_i_url;
  661. }else{
  662. unset($arr[$k]);
  663. }
  664. }
  665. return array_unique($arr);
  666. }
  667. //获取淘宝商品描述
  668. public function getTaobaoDesc($html = '')
  669. {
  670. preg_match('/descUrl([^<>]*)counterApi/', $html, $descarr);
  671. if(!isset($descarr[1])) return '';
  672. $arr = explode(':', $descarr[1]);
  673. $url = [];
  674. foreach($arr as $k => $v)
  675. {
  676. if(strpos($v,'//'))
  677. {
  678. $str = str_replace(['\'',',',' ','?',':'], '', $v);
  679. $url[] = trim($str);
  680. }
  681. }
  682. if($url)
  683. {
  684. return strpos($url[0],'http') ? $url[0] : 'http:' . $url[0];
  685. }else{
  686. return '';
  687. }
  688. }
  689. /**
  690. * GET 请求
  691. * @param string $url
  692. */
  693. public function curl_Get($url = '',$time_out = 25)
  694. {
  695. if(!$url) return '';
  696. $ch = curl_init();
  697. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  698. if(stripos($url,"https://")!==FALSE)
  699. {
  700. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  701. }
  702. curl_setopt($ch, CURLOPT_URL, $url);
  703. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:'.$_SERVER['HTTP_USER_AGENT']));
  704. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  705. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  706. curl_setopt($ch, CURLOPT_TIMEOUT,$time_out);
  707. $response = curl_exec($ch);
  708. if($error=curl_error($ch)){
  709. return false;
  710. }
  711. curl_close($ch);
  712. return mb_convert_encoding($response, 'utf-8','GB2312');
  713. }
  714. //检测远程文件是否存在
  715. public function _img_exists($url = '')
  716. {
  717. ini_set("max_execution_time", 0);
  718. $str = @file_get_contents($url,0,null,0,1);
  719. if(strlen($str) <= 0) return false;
  720. if($str)
  721. return true;
  722. else
  723. return false;
  724. }
  725. //TODO 下载图片
  726. public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
  727. {
  728. if(!strlen(trim($url))) return '';
  729. if(!strlen(trim($name)))
  730. {
  731. //TODO 获取要下载的文件名称
  732. $downloadImageInfo = $this->getImageExtname($url);
  733. $name = $downloadImageInfo['file_name'];
  734. if(!strlen(trim($name))) return '';
  735. }
  736. //TODO 获取远程文件所采用的方法
  737. if($type){
  738. $ch = curl_init();
  739. curl_setopt($ch, CURLOPT_URL, $url);
  740. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  741. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  742. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //TODO 跳过证书检查
  743. if(stripos($url,"https://") !== FALSE) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //TODO 从证书中检查SSL加密算法是否存在
  744. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:'.$_SERVER['HTTP_USER_AGENT']));
  745. if(ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);//TODO 是否采集301、302之后的页面
  746. $content = curl_exec($ch);
  747. curl_close($ch);
  748. }else{
  749. try{
  750. ob_start();
  751. readfile($url);
  752. $content = ob_get_contents();
  753. ob_end_clean();
  754. }catch (\Exception $e){
  755. return $e->getMessage();
  756. }
  757. }
  758. $size = strlen(trim($content));
  759. if(!$content || $size <= 2) return '图片流获取失败';
  760. $date_dir = date('Y') . DS . date('m') . DS . date('d');
  761. $imageInfo = UploadService::imageStream($name, $content, 'attach' . DS . $date_dir. DS);
  762. if(!is_array($imageInfo)) return $imageInfo;
  763. $date['path'] = $imageInfo['dir'];
  764. $date['name'] = $imageInfo['name'];
  765. $date['size'] = $imageInfo['size'];
  766. $date['mime'] = $imageInfo['type'];
  767. $date['image_type'] = $imageInfo['image_type'];
  768. $date['is_exists'] = false;
  769. return $date;
  770. }
  771. //获取即将要下载的图片扩展名
  772. public function getImageExtname($url = '', $ex = 'jpg')
  773. {
  774. $_empty = ['file_name' => '', 'ext_name' => $ex];
  775. if(!$url) return $_empty;
  776. if(strpos($url,'?'))
  777. {
  778. $_tarr = explode('?',$url);
  779. $url = trim($_tarr[0]);
  780. }
  781. $arr = explode('.',$url);
  782. if(!is_array($arr) || count($arr) <= 1) return $_empty;
  783. $ext_name = trim($arr[count($arr)-1]);
  784. $ext_name = !$ext_name ? $ex : $ext_name;
  785. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  786. }
  787. /*
  788. $filepath = 绝对路径,末尾有斜杠 /
  789. $name = 图片文件名
  790. $maxwidth 定义生成图片的最大宽度(单位:像素)
  791. $maxheight 生成图片的最大高度(单位:像素)
  792. $filetype 最终生成的图片类型(.jpg/.png/.gif)
  793. */
  794. public function resizeImage($filepath = '',$name = '', $maxwidth = 0,$maxheight = 0)
  795. {
  796. $pic_file = $filepath . $name; //图片文件
  797. $img_info = getimagesize($pic_file); //索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,
  798. if($img_info[2] == 1)
  799. {
  800. $im = imagecreatefromgif($pic_file); //打开图片
  801. $filetype = '.gif';
  802. }elseif($img_info[2] == 2){
  803. $im = imagecreatefromjpeg($pic_file); //打开图片
  804. $filetype = '.jpg';
  805. }elseif($img_info[2] == 3){
  806. $im = imagecreatefrompng($pic_file); //打开图片
  807. $filetype = '.png';
  808. }else{
  809. return ['path' => $filepath, 'file' => $name, 'mime' => ''];
  810. }
  811. $file_name = md5('_tmp_' . microtime() . '_' .rand(0,10)) . $filetype;
  812. $pic_width = imagesx($im);
  813. $pic_height = imagesy($im);
  814. $resizewidth_tag = false;
  815. $resizeheight_tag = false;
  816. if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
  817. {
  818. if($maxwidth && $pic_width>$maxwidth)
  819. {
  820. $widthratio = $maxwidth/$pic_width;
  821. $resizewidth_tag = true;
  822. }
  823. if($maxheight && $pic_height>$maxheight)
  824. {
  825. $heightratio = $maxheight/$pic_height;
  826. $resizeheight_tag = true;
  827. }
  828. if($resizewidth_tag && $resizeheight_tag)
  829. {
  830. if($widthratio<$heightratio)
  831. $ratio = $widthratio;
  832. else
  833. $ratio = $heightratio;
  834. }
  835. if($resizewidth_tag && !$resizeheight_tag)
  836. $ratio = $widthratio;
  837. if($resizeheight_tag && !$resizewidth_tag)
  838. $ratio = $heightratio;
  839. $newwidth = $pic_width * $ratio;
  840. $newheight = $pic_height * $ratio;
  841. if(function_exists("imagecopyresampled"))
  842. {
  843. $newim = imagecreatetruecolor($newwidth,$newheight);
  844. imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
  845. }else{
  846. $newim = imagecreate($newwidth,$newheight);
  847. imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
  848. }
  849. if($filetype == '.png')
  850. {
  851. imagepng($newim,$filepath . $file_name);
  852. }else if($filetype == '.gif'){
  853. imagegif($newim,$filepath . $file_name);
  854. }else{
  855. imagejpeg($newim,$filepath . $file_name);
  856. }
  857. imagedestroy($newim);
  858. }else{
  859. if($filetype == '.png')
  860. {
  861. imagepng($im,$filepath . $file_name);
  862. }else if($filetype == '.gif'){
  863. imagegif($im,$filepath . $file_name);
  864. }else{
  865. imagejpeg($im,$filepath . $file_name);
  866. }
  867. imagedestroy($im);
  868. }
  869. @unlink($pic_file);
  870. return ['path' => $filepath, 'file' => $file_name, 'mime' => $img_info['mime']];
  871. }
  872. }