CopyTaobao.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 crmeb\services\HttpService;
  9. use crmeb\services\UploadService;
  10. use think\exception\PDOException;
  11. use app\admin\controller\AuthController;
  12. use app\admin\model\system\SystemConfig;
  13. use crmeb\traits\CurdControllerTrait;
  14. use crmeb\services\JsonService;
  15. use crmeb\services\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(null, 1);
  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::create(['pid' => '0', 'name' => $this->AttachmentCategoryName, 'enname' => '']);
  320. //生成附件目录
  321. try{
  322. if (make_path('attach', 3) == '') return JsonService::fail('无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS. 'attach' . DS);
  323. }catch (\Exception $e){
  324. return JsonService::fail($e->getMessage().'或无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS. 'attach' . DS);
  325. }
  326. ini_set("max_execution_time", 600);
  327. //开始图片下载处理
  328. ProductModel::beginTrans();
  329. try {
  330. //放入主图
  331. $images = [
  332. ['w' => 305, 'h' => 305, 'line' => $data['image'], 'valuename' => 'image']
  333. ];
  334. //放入轮播图
  335. foreach ($data['slider_image'] as $item) {
  336. $value = ['w' => 640, 'h' => 640, 'line' => $item, 'valuename' => 'slider_image', 'isTwoArray' => true];
  337. array_push($images, $value);
  338. }
  339. //执行下载
  340. $res = $this->uploadImage($images, false, 0, $AttachmentCategory['id']);
  341. if (!is_array($res)) return JsonService::fail($this->errorInfo ? $this->errorInfo : '保存图片失败');
  342. if (isset($res['image'])) $data['image'] = $res['image'];
  343. if (isset($res['slider_image'])) $data['slider_image'] = $res['slider_image'];
  344. $data['slider_image'] = count($data['slider_image']) ? json_encode($data['slider_image']) : '';
  345. //替换并下载详情里面的图片默认下载全部图片
  346. $data['description'] = preg_replace('#<style>.*?</style>#is', '', $data['description']);
  347. $data['description'] = $this->uploadImage($data['description_images'], $data['description'], 1, $AttachmentCategory['id']);
  348. unset($data['description_images']);
  349. $data['add_time'] = time();
  350. $cate_id = explode(',', $data['cate_id']);
  351. //产品存在
  352. if ($productInfo = ProductModel::where('soure_link', $data['soure_link'])->find()) {
  353. $productInfo->description = $data['description'];
  354. $productInfo->slider_image = $data['slider_image'];
  355. $productInfo->image = $data['image'];
  356. $productInfo->store_name = $data['store_name'];
  357. $productInfo->save();
  358. ProductModel::commitTrans();
  359. return JsonService::successful('商品存在,信息已被更新成功');
  360. } else {
  361. //不存在时新增
  362. if ($res = ProductModel::create($data)) {
  363. foreach ($cate_id as $cid) {
  364. \think\facade\Db::name('store_product_cate')->insert(['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()]);
  365. }
  366. ProductModel::commitTrans();
  367. return JsonService::successful('生成产品成功');
  368. } else {
  369. ProductModel::rollbackTrans();
  370. return JsonService::fail('生成产品失败');
  371. }
  372. }
  373. } catch (PDOException $e) {
  374. ProductModel::rollbackTrans();
  375. return JsonService::fail('插入数据库错误', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  376. } catch (\Exception $e) {
  377. ProductModel::rollbackTrans();
  378. return JsonService::fail('系统错误', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  379. }
  380. }
  381. /*
  382. * 上传图片处理
  383. * @param array $image 图片路径
  384. * @param int $uploadType 上传方式 0=远程下载
  385. * */
  386. public function uploadImage(array $images = [], $html = '', $uploadType = 0, $AttachmentCategoryId = 0)
  387. {
  388. $uploadImage = [];
  389. $siteUrl = SystemConfig::getConfigValue('site_url');
  390. switch ($uploadType) {
  391. case 0:
  392. foreach ($images as $item) {
  393. //下载图片文件
  394. if ($item['w'] && $item['h'])
  395. $uploadValue = $this->downloadImage($item['line'], '', 0, 30, $item['w'], $item['h']);
  396. else
  397. $uploadValue = $this->downloadImage($item['line']);
  398. //下载成功更新数据库
  399. if (is_array($uploadValue)) {
  400. //TODO 拼接图片地址
  401. if ($uploadValue['image_type'] == 1) $imagePath = $siteUrl . $uploadValue['path'];
  402. else $imagePath = $uploadValue['path'];
  403. //写入数据库
  404. if (!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'], $uploadValue['size'], $uploadValue['mime'], $imagePath, $imagePath, $AttachmentCategoryId, $uploadValue['image_type'], time(), 1);
  405. //组装数组
  406. if (isset($item['isTwoArray']) && $item['isTwoArray'])
  407. $uploadImage[$item['valuename']][] = $imagePath;
  408. else
  409. $uploadImage[$item['valuename']] = $imagePath;
  410. }
  411. }
  412. break;
  413. case 1:
  414. preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $html, $match);
  415. if (isset($match[1])) {
  416. foreach ($match[1] as $item) {
  417. if (is_int(strpos($item, 'http')))
  418. $arcurl = $item;
  419. else
  420. $arcurl = 'http://' . ltrim($item, '\//');
  421. $uploadValue = $this->downloadImage($arcurl);
  422. //下载成功更新数据库
  423. if (is_array($uploadValue)) {
  424. //TODO 拼接图片地址
  425. if ($uploadValue['image_type'] == 1) $imagePath = $siteUrl . $uploadValue['path'];
  426. else $imagePath = $uploadValue['path'];
  427. //写入数据库
  428. if (!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'], $uploadValue['size'], $uploadValue['mime'], $imagePath, $imagePath, $AttachmentCategoryId, $uploadValue['image_type'], time(), 1);
  429. //替换图片
  430. $html = str_replace($item, $imagePath, $html);
  431. } else {
  432. //替换掉没有下载下来的图片
  433. $html = preg_replace('#<img.*?src="' . $item . '"*>#i', '', $html);
  434. }
  435. }
  436. }
  437. return $html;
  438. break;
  439. default:
  440. return $this->setErrorInfo('上传方式错误');
  441. break;
  442. }
  443. return $uploadImage;
  444. }
  445. //提取商品描述中的所有图片
  446. public function decodedesc($desc = '')
  447. {
  448. $desc = trim($desc);
  449. if (!$desc) return '';
  450. preg_match_all('/<img[^>]*?src="([^"]*?)"[^>]*?>/i', $desc, $match);
  451. if (!isset($match[1]) || count($match[1]) <= 0) {
  452. preg_match_all('/:url(([^"]*?));/i', $desc, $match);
  453. if (!isset($match[1]) || count($match[1]) <= 0) return $desc;
  454. } else {
  455. preg_match_all('/:url(([^"]*?));/i', $desc, $newmatch);
  456. if (isset($newmatch[1]) && count($newmatch[1]) > 0) $match[1] = array_merge($match[1], $newmatch[1]);
  457. }
  458. $match[1] = array_unique($match[1]); //去掉重复
  459. foreach ($match[1] as $k => &$v) {
  460. $_tmp_img = str_replace([')', '(', ';'], '', $v);
  461. $_tmp_img = strpos($_tmp_img, 'http') ? $_tmp_img : 'http:' . $_tmp_img;
  462. if (strpos($v, '?')) {
  463. $_tarr = explode('?', $v);
  464. $_tmp_img = trim($_tarr[0]);
  465. }
  466. $_urls = str_replace(['\'', '"'], '', $_tmp_img);
  467. if ($this->_img_exists($_urls)) $v = $_urls;
  468. }
  469. return $match[1];
  470. }
  471. //获取京东商品组图
  472. public function getJdImg($html = '')
  473. {
  474. //获取图片服务器网址
  475. preg_match('/<img(.*?)id="spec-img"(.*?)data-origin=\"(.*?)\"[^>]*>/', $html, $img);
  476. if (!isset($img[3])) return '';
  477. $info = parse_url(trim($img[3]));
  478. if (!$info['host']) return '';
  479. if (!$info['path']) return '';
  480. $_tmparr = explode('/', trim($info['path']));
  481. $url = 'http://' . $info['host'] . '/' . $_tmparr[1] . '/' . str_replace(['jfs', ' '], '', trim($_tmparr[2]));
  482. preg_match('/imageList:(.*?)"],/is', $html, $img);
  483. if (!isset($img[1])) {
  484. return '';
  485. }
  486. $_arr = explode(',', $img[1]);
  487. foreach ($_arr as $k => &$v) {
  488. $_str = $url . str_replace(['"', '[', ']', ' '], '', trim($v));
  489. if (strpos($_str, '?')) {
  490. $_tarr = explode('?', $_str);
  491. $_str = trim($_tarr[0]);
  492. }
  493. if ($this->_img_exists($_str)) {
  494. $v = $_str;
  495. } else {
  496. unset($_arr[$k]);
  497. }
  498. }
  499. return array_unique($_arr);
  500. }
  501. //获取京东商品描述
  502. public function getJdDesc($html = '')
  503. {
  504. preg_match('/,(.*?)desc:([^<>]*)\',/i', $html, $descarr);
  505. if (!isset($descarr[1]) && !isset($descarr[2])) return '';
  506. $tmpArr = explode(',', $descarr[2]);
  507. if (count($tmpArr) > 0) {
  508. $descarr[2] = trim($tmpArr[0]);
  509. }
  510. $replace_arr = ['\'', '\',', ' ', ',', '/*', '*/'];
  511. if (isset($descarr[2])) {
  512. $d_url = str_replace($replace_arr, '', $descarr[2]);
  513. return $this->formatDescUrl(strpos($d_url, 'http') ? $d_url : 'http:' . $d_url);
  514. }
  515. $d_url = str_replace($replace_arr, '', $descarr[1]);
  516. $d_url = $this->formatDescUrl($d_url);
  517. $d_url = rtrim(rtrim($d_url, "?"), "&");
  518. return substr($d_url, 0, 4) == 'http' ? $d_url : 'http:' . $d_url;
  519. }
  520. //处理下京东商品描述网址
  521. public function formatDescUrl($url = '')
  522. {
  523. if (!$url) return '';
  524. $url = substr($url, 0, 4) == 'http' ? $url : 'http:' . $url;
  525. if (!strpos($url, '&')) {
  526. $_arr = explode('?', $url);
  527. if (!is_array($_arr) || count($_arr) <= 0) return $url;
  528. return trim($_arr[0]);
  529. } else {
  530. $_arr = explode('&', $url);
  531. }
  532. if (!is_array($_arr) || count($_arr) <= 0) return $url;
  533. unset($_arr[count($_arr) - 1]);
  534. $new_url = '';
  535. foreach ($_arr as $k => $v) {
  536. $new_url .= $v . '&';
  537. }
  538. return !$new_url ? $url : $new_url;
  539. }
  540. //获取1688商品组图
  541. public function get1688Img($html = '')
  542. {
  543. preg_match('/<ul class=\"nav nav-tabs fd-clr\">(.*?)<\/ul>/is', $html, $img);
  544. if (!isset($img[0])) {
  545. return '';
  546. }
  547. preg_match_all('/preview":"(.*?)\"\}\'>/is', $img[0], $arrb);
  548. if (!isset($arrb[1]) || count($arrb[1]) <= 0) {
  549. return '';
  550. }
  551. $thumb = [];
  552. $gaoqing = [];
  553. $res = ['thumb' => '', 'gaoqing' => '']; //缩略图片和高清图片
  554. foreach ($arrb[1] as $k => $v) {
  555. $_str = str_replace(['","original":"'], '*', $v);
  556. $_arr = explode('*', $_str);
  557. if (is_array($_arr) && isset($_arr[0]) && isset($_arr[1])) {
  558. if (strpos($_arr[0], '?')) {
  559. $_tarr = explode('?', $_arr[0]);
  560. $_arr[0] = trim($_tarr[0]);
  561. }
  562. if (strpos($_arr[1], '?')) {
  563. $_tarr = explode('?', $_arr[1]);
  564. $_arr[1] = trim($_tarr[0]);
  565. }
  566. if ($this->_img_exists($_arr[0])) $thumb[] = trim($_arr[0]);
  567. if ($this->_img_exists($_arr[1])) $gaoqing[] = trim($_arr[1]);
  568. }
  569. }
  570. $res = ['thumb' => array_unique($thumb), 'gaoqing' => array_unique($gaoqing)]; //缩略图片和高清图片
  571. return $res;
  572. }
  573. //获取1688商品描述
  574. public function get1688Desc($html = '')
  575. {
  576. preg_match('/data-tfs-url="([^<>]*)data-enable="true"/', $html, $descarr);
  577. if (!isset($descarr[1])) return '';
  578. return str_replace(['"', ' '], '', $descarr[1]);
  579. }
  580. //获取天猫商品组图
  581. public function getTianMaoImg($html = '')
  582. {
  583. $pic_size = '430';
  584. preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $html, $img);
  585. if (isset($img[1])) {
  586. $_arr = explode('x', $img[1]);
  587. $filename = $_arr[count($_arr) - 1];
  588. $pic_size = intval(substr($filename, 0, 3));
  589. }
  590. preg_match('|<ul id="J_UlThumb" class="tb-thumb tm-clear">(.*)</ul>|isU', $html, $match);
  591. preg_match_all('/<img src="(.*?)" \//', $match[1], $images);
  592. if (!isset($images[1])) return '';
  593. foreach ($images[1] as $k => &$v) {
  594. $tmp_v = trim($v);
  595. $_arr = explode('x', $tmp_v);
  596. $_fname = $_arr[count($_arr) - 1];
  597. $_size = intval(substr($_fname, 0, 3));
  598. if (strpos($tmp_v, '://')) {
  599. $_arr = explode(':', $tmp_v);
  600. $r_url = trim($_arr[1]);
  601. } else {
  602. $r_url = $tmp_v;
  603. }
  604. $str = str_replace($_size, $pic_size, $r_url);
  605. if (strpos($str, '?')) {
  606. $_tarr = explode('?', $str);
  607. $str = trim($_tarr[0]);
  608. }
  609. $_i_url = strpos($str, 'http') ? $str : 'http:' . $str;
  610. if ($this->_img_exists($_i_url)) {
  611. $v = $_i_url;
  612. } else {
  613. unset($images[1][$k]);
  614. }
  615. }
  616. return array_unique($images[1]);
  617. }
  618. //获取天猫商品描述
  619. public function getTianMaoDesc($html = '')
  620. {
  621. preg_match('/descUrl":"([^<>]*)","httpsDescUrl":"/', $html, $descarr);
  622. if (!isset($descarr[1])) {
  623. preg_match('/httpsDescUrl":"([^<>]*)","fetchDcUrl/', $html, $descarr);
  624. if (!isset($descarr[1])) return '';
  625. }
  626. return strpos($descarr[1], 'http') ? $descarr[1] : 'http:' . $descarr[1];
  627. }
  628. //获取淘宝商品组图
  629. public function getTaobaoImg($html = '')
  630. {
  631. preg_match('/auctionImages([^<>]*)"]/', $html, $imgarr);
  632. if (!isset($imgarr[1])) return '';
  633. $arr = explode(',', $imgarr[1]);
  634. foreach ($arr as $k => &$v) {
  635. $str = trim($v);
  636. $str = str_replace(['"', ' ', '', ':['], '', $str);
  637. if (strpos($str, '?')) {
  638. $_tarr = explode('?', $str);
  639. $str = trim($_tarr[0]);
  640. }
  641. $_i_url = strpos($str, 'http') ? $str : 'http:' . $str;
  642. if ($this->_img_exists($_i_url)) {
  643. $v = $_i_url;
  644. } else {
  645. unset($arr[$k]);
  646. }
  647. }
  648. return array_unique($arr);
  649. }
  650. //获取淘宝商品描述
  651. public function getTaobaoDesc($html = '')
  652. {
  653. preg_match('/descUrl([^<>]*)counterApi/', $html, $descarr);
  654. if (!isset($descarr[1])) return '';
  655. $arr = explode(':', $descarr[1]);
  656. $url = [];
  657. foreach ($arr as $k => $v) {
  658. if (strpos($v, '//')) {
  659. $str = str_replace(['\'', ',', ' ', '?', ':'], '', $v);
  660. $url[] = trim($str);
  661. }
  662. }
  663. if ($url) {
  664. return strpos($url[0], 'http') ? $url[0] : 'http:' . $url[0];
  665. } else {
  666. return '';
  667. }
  668. }
  669. /**
  670. * GET 请求
  671. * @param string $url
  672. */
  673. public function curl_Get($url = '', $time_out = 25)
  674. {
  675. if (!$url) return '';
  676. $ch = curl_init();
  677. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  678. if (stripos($url, "https://") !== FALSE) {
  679. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  680. }
  681. curl_setopt($ch, CURLOPT_URL, $url);
  682. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  683. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  684. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  685. curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
  686. $response = curl_exec($ch);
  687. if ($error = curl_error($ch)) {
  688. return false;
  689. }
  690. curl_close($ch);
  691. return mb_convert_encoding($response, 'utf-8', 'GB2312');
  692. }
  693. //检测远程文件是否存在
  694. public function _img_exists($url = '')
  695. {
  696. ini_set("max_execution_time", 0);
  697. $str = @file_get_contents($url, 0, null, 0, 1);
  698. if (strlen($str) <= 0) return false;
  699. if ($str)
  700. return true;
  701. else
  702. return false;
  703. }
  704. //TODO 下载图片
  705. public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
  706. {
  707. if (!strlen(trim($url))) return '';
  708. if (!strlen(trim($name))) {
  709. //TODO 获取要下载的文件名称
  710. $downloadImageInfo = $this->getImageExtname($url);
  711. $name = $downloadImageInfo['file_name'];
  712. if (!strlen(trim($name))) return '';
  713. }
  714. //TODO 获取远程文件所采用的方法
  715. if ($type) {
  716. $ch = curl_init();
  717. curl_setopt($ch, CURLOPT_URL, $url);
  718. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  719. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  720. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //TODO 跳过证书检查
  721. if (stripos($url, "https://") !== FALSE) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //TODO 从证书中检查SSL加密算法是否存在
  722. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  723. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//TODO 是否采集301、302之后的页面
  724. $content = curl_exec($ch);
  725. curl_close($ch);
  726. } else {
  727. try {
  728. ob_start();
  729. readfile($url);
  730. $content = ob_get_contents();
  731. ob_end_clean();
  732. } catch (\Exception $e) {
  733. return $e->getMessage();
  734. }
  735. }
  736. $size = strlen(trim($content));
  737. if (!$content || $size <= 2) return '图片流获取失败';
  738. $date_dir = date('Y') . DS . date('m') . DS . date('d');
  739. $imageInfo = UploadService::imageStream($name, $content, 'attach/' . $date_dir);
  740. if (!is_array($imageInfo)) return $imageInfo;
  741. $date['path'] = $imageInfo['dir'];
  742. $date['name'] = $imageInfo['name'];
  743. $date['size'] = $imageInfo['size'];
  744. $date['mime'] = $imageInfo['type'];
  745. $date['image_type'] = $imageInfo['image_type'];
  746. $date['is_exists'] = false;
  747. return $date;
  748. }
  749. //获取即将要下载的图片扩展名
  750. public function getImageExtname($url = '', $ex = 'jpg')
  751. {
  752. $_empty = ['file_name' => '', 'ext_name' => $ex];
  753. if (!$url) return $_empty;
  754. if (strpos($url, '?')) {
  755. $_tarr = explode('?', $url);
  756. $url = trim($_tarr[0]);
  757. }
  758. $arr = explode('.', $url);
  759. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  760. $ext_name = trim($arr[count($arr) - 1]);
  761. $ext_name = !$ext_name ? $ex : $ext_name;
  762. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  763. }
  764. /*
  765. $filepath = 绝对路径,末尾有斜杠 /
  766. $name = 图片文件名
  767. $maxwidth 定义生成图片的最大宽度(单位:像素)
  768. $maxheight 生成图片的最大高度(单位:像素)
  769. $filetype 最终生成的图片类型(.jpg/.png/.gif)
  770. */
  771. public function resizeImage($filepath = '', $name = '', $maxwidth = 0, $maxheight = 0)
  772. {
  773. $pic_file = $filepath . $name; //图片文件
  774. $img_info = getimagesize($pic_file); //索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,
  775. if ($img_info[2] == 1) {
  776. $im = imagecreatefromgif($pic_file); //打开图片
  777. $filetype = '.gif';
  778. } elseif ($img_info[2] == 2) {
  779. $im = imagecreatefromjpeg($pic_file); //打开图片
  780. $filetype = '.jpg';
  781. } elseif ($img_info[2] == 3) {
  782. $im = imagecreatefrompng($pic_file); //打开图片
  783. $filetype = '.png';
  784. } else {
  785. return ['path' => $filepath, 'file' => $name, 'mime' => ''];
  786. }
  787. $file_name = md5('_tmp_' . microtime() . '_' . rand(0, 10)) . $filetype;
  788. $pic_width = imagesx($im);
  789. $pic_height = imagesy($im);
  790. $resizewidth_tag = false;
  791. $resizeheight_tag = false;
  792. if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
  793. if ($maxwidth && $pic_width > $maxwidth) {
  794. $widthratio = $maxwidth / $pic_width;
  795. $resizewidth_tag = true;
  796. }
  797. if ($maxheight && $pic_height > $maxheight) {
  798. $heightratio = $maxheight / $pic_height;
  799. $resizeheight_tag = true;
  800. }
  801. if ($resizewidth_tag && $resizeheight_tag) {
  802. if ($widthratio < $heightratio)
  803. $ratio = $widthratio;
  804. else
  805. $ratio = $heightratio;
  806. }
  807. if ($resizewidth_tag && !$resizeheight_tag)
  808. $ratio = $widthratio;
  809. if ($resizeheight_tag && !$resizewidth_tag)
  810. $ratio = $heightratio;
  811. $newwidth = $pic_width * $ratio;
  812. $newheight = $pic_height * $ratio;
  813. if (function_exists("imagecopyresampled")) {
  814. $newim = imagecreatetruecolor($newwidth, $newheight);
  815. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  816. } else {
  817. $newim = imagecreate($newwidth, $newheight);
  818. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  819. }
  820. if ($filetype == '.png') {
  821. imagepng($newim, $filepath . $file_name);
  822. } else if ($filetype == '.gif') {
  823. imagegif($newim, $filepath . $file_name);
  824. } else {
  825. imagejpeg($newim, $filepath . $file_name);
  826. }
  827. imagedestroy($newim);
  828. } else {
  829. if ($filetype == '.png') {
  830. imagepng($im, $filepath . $file_name);
  831. } else if ($filetype == '.gif') {
  832. imagegif($im, $filepath . $file_name);
  833. } else {
  834. imagejpeg($im, $filepath . $file_name);
  835. }
  836. imagedestroy($im);
  837. }
  838. @unlink($pic_file);
  839. return ['path' => $filepath, 'file' => $file_name, 'mime' => $img_info['mime']];
  840. }
  841. }