现如今,微信的应用无处不在,也时刻都在影响着人们的生活,特别是对于爱玩微信的朋友来说,微信聊天,微信红包, 微信公众号的文章阅读,都渐渐的成为广大用户们饭后的娱乐之一,这里也就不得不提一下,微信文章,现在的许多的项目里有许多各种各样的需求,刚好这次开发遇到了一个关于抓取微信公众号文章的问题。上网百度也找到五花八门的源码, 后经过总结与代码的优化,实现了通过微信的文章来抓取微信文章的内容.
/**
* 获取威信文章的图片并且加以存储到本地服务器
* @param $url
* @param $path
* @param $name
* @return string
*/
private function createPhoto($url, $path, $name) {
$img = file_get_contents ( $url );
$info = getimagesize ( $url );
$type = str_replace ( ‘image/’, ”, $info [‘mime’] );
$fileName = $path . DIRECTORY_SEPARATOR . $name . ".$type";
file_put_contents ( $fileName, $img );
return $fileName;
}
/**
* 抓取的微信文章的主题内容
* @param $url
* @param $path
* @return mixed
*/
private function transform($url, $path) {
if (! file_exists ( $path ))
mkdir ( $path );
$data [‘url’] = $url; // 文章URL
$content = file_get_contents ( $url );
preg_match ( ‘/
(.*)/i’, $content, $result );$data [‘title’] = $result [1]; // 文章标题
preg_match ( ‘/var\s+msg_cdn_url\s*=\s*"([^\s]*)"/’, $content, $result );
$data [‘cover’] = $result [1]; // 封面
preg_match ( ‘/var\s+nickname\s*=\s*"([^\s]*)"/’, $content, $result );
$data [‘nickname’] = $result [1]; // 公众号昵称
preg_match ( ‘/var\s+ct\s*=\s*"([^\s]*)"/’, $content, $result );
$data [‘ct’] = $result [1]; // 公众号发布的时间戳
preg_match ( ‘/var\s+user_name\s*=\s*"([^\s]*)"/’, $content, $result );
$data [‘user_name’] = $result [1]; // 公众号的原始ID
preg_match ( ‘/var\s+round_head_img\s*=\s*"([^\s]*)"/’, $content, $result );
$data [’round_head_img’] = $this->createPic ( $result [1], $path, "round_head_img_" . $data [‘user_name’] ); // // 公众号头像
preg_match ( "/s?__biz=(.*)&mid=/i", $url, $result );
$data [‘bizId’] = $result [1]; // 公众号BizId
preg_match ( ‘/var\s+msg_desc\s*=\s*"([^\s]*)"/’, $content, $result );
$data [‘msg_desc’] = $result [1]; // 公众号文章摘要
// 获取微信主体内容
preg_match ( ‘/
preg_match_all ( ‘/data-src="[a-zA-z]+:\/\/[^\s]*[mmbiz|mmbiz_jpg|mmbiz_gif|mmbiz_png|mmbiz_jpeg]\/[^\s]*\/\d+\?[^\s]*=[^\s]*"|data-src="[a-zA-z]+:\/\/[^\s]*[mmbiz|mmbiz_jpg|mmbiz_gif|mmbiz_png|mmbiz_jpeg]\/[^\s]*\/\d+"|data-src="[a-zA-z]+:\/\/[^\s]*[mmbiz|mmbiz_jpg|mmbiz_gif|mmbiz_png|mmbiz_jpeg]\/[^\s]*\/\d+[\?]"|background-image\s*:\s*url\s*\(\s*[a-zA-z]+:\/\/[^\s]*mmbiz\/[^\s]*\/\d+|background-image\s*:\s*url\s*\(\s*[a-zA-z]+:\/\/[^\s]*mmbiz\/[^\s]*\/\d+\?[^\s]*=[^\s]*/is’, $result [1], $result2 );
// 判断微信主体中是否包含防盗链图片
if (! empty ( $result2 [0] )) {
foreach ( $result2 [0] as $value ) {
preg_match ( ‘/[a-zA-z]+:\/\/[^\s]*\/[mmbiz|mmbiz_jpg|mmbiz_gif|mmbiz_png|mmbiz_jpeg]\/([^\s\/]*)\/\d+\?[^\s"]*|[a-zA-z]+:\/\/[^\s]*[mmbiz|mmbiz_jpg|mmbiz_gif|mmbiz_png|mmbiz_jpeg]\/([^\s\/]*)\/\d+/’, $value, $temp );
$temp = array_filter ( $temp );
$temp = $this->ksort ( $temp );
$urlList [] = $temp [0];
$nameList [] = $temp [1];
}
foreach ( $urlList as $value ) {
$name = array_shift ( $nameList );
$fileName = $this->createPic ( $value, $path, $name ); // 保存为本地图片
$result [1] = str_replace ( $value, $fileName, $result [1] );
}
}
// 更新所有data-src的地址
$result [1] = str_replace ( "data-src", "src", $result [1] );
// 返回处理后的微信主体内容。
$data [‘content’] = trim($result [1]);
$data[‘img’] = $result2;
return $data;
}
其实代码部分都是通过正则表达式来进行匹配的,希望对大家有所帮助.
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/18780.html
微信打赏
支付宝打赏
感谢您对作者Galen的打赏,我们会更加努力! 如果您想成为作者,请点我