Files
tuiguangzhushou/application/system/controller/Test.php
2020-08-06 15:26:41 +08:00

93 lines
4.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace app\system\controller;
class Test extends _Init
{
public function index($title = '', $type = '')
{
$html = file_get_contents("https://mp.weixin.qq.com/s/IiWlqrRf1eSDuAmhI0fFQw");
// $html = http("https://mp.weixin.qq.com/s/IiWlqrRf1eSDuAmhI0fFQw", 'GET', '', '', $userAgent);
//获取文章标题
preg_match_all("/id=\"activity-name\">(.*)<\/h2>/is", $html, $title);
//获取文章内容部分
preg_match_all("/id=\"js_content\">(.*)<script/iUs", $html, $content, PREG_PATTERN_ORDER);
//格式化标题
//拼接正确的内容标签
$content = "<div id='js_content' class='ce-padding-sm'>" . $content[1][0];
//将所有图片的data-src更改为src
// $content = str_replace("data-src", "src", $content);
$content = str_replace("/640?", "/0?", $content);
// $content = str_replace('0?"', '0"', $content);
//获取所有图片地址
// preg_match_all($parrent, $content, $img);
preg_match_all('/<[img|IMG].*?>/is', $content, $img);
//遍历所有图片,采集到服务器
$i = 0;
$thumb = '';
$img_pre = uniqid(); // 确保每篇文章的图片前缀一致,路径按照日期存储
$dir = './uploads/collect/' . date('Y-m/d/');
echo '<style>img{width:100%;}</style>';
foreach ($img[0] as $key => $value) {
//判断图片是否存在http头
preg_match_all('/data-src=".*?"/is', $value, $imim);
$vv = $imim[0][0];
$str = explode('"', $vv);
$fstr = explode('/', $str[1]);
$allname = $fstr[4];
if (strrpos($str[1], 'wx_fmt=') > 0) {
$ext = substr($str[1], strrpos($str[1], 'wx_fmt=') + 7);
$allname .= '.' . $ext;
}
$image = file_get_contents($str[1]);
$realfile = $dir . $allname;
file_put_contents($realfile, $image);
$content = str_replace($value, '<img src="' . $realfile . '">', $content);
// if (preg_match('/(http:\/\/)|(https:\/\/)/i', $value)) {
// //分割微信图片地址
// $str = explode('/', $value);
// //获取图片地址名称,不包括拓展名
// if (count($str) < 5) {
// continue;
// }
// // $allname = $str[4];
// $allname = $str[5];
// //识别拓展名
// if (strrpos($value, 'wx_fmt=') > 0) {
// $ext = substr($value, strrpos($value, 'wx_fmt=') + 7);
// $allname = $img_pre . '_' . $i . '.' . $ext;
// }
// //拓展名拼接图片名称
// //获取图片数据
// // $image = file_get_contents($value);
// $image = http($value, 'GET', '', '', $userAgent);
// //保存服务器目录文件
// try {
// if (!is_dir($dir)) {
// mkdir($dir, 0755, true);
// }
// file_put_contents($dir . $allname, $image);
// //更替图片文件地址为服务器图片地址
// $content = str_replace($value, ltrim($dir, '.') . $allname, $content);
// if ($i == 0) {
// $thumb = ltrim($dir, '.') . $allname;
// }
// $i++;
// } catch (Exception $e) {
// continue;
// }
// }
}
echo $content;
}
}