php保存微信文章到本地,同时自动下载微信里面的图片到本地
function get_wx_content($url) {
$ch = curl_init();
$headers = array();
$headers[] = 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7,ja;q=0.6';
$headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 ;Safari/537.36';
$headers[] = 'Accept: application/json, text/javascript, */*; q=0.01';
$headers[] = 'Referer: https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/;appmsg_edit_v2&action=edit&isNew=1&type=10&token=1112731269&lang=zh_CN';
$headers[] = 'X-Requested-With: XMLHttpRequest';
$headers[] = 'Connection: keep-alive';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$content = curl_exec($ch);
return $content;
}
function image_wx_handle($match_url,$prefix=null) {
var_dump($match_url);
$imgname = "img/".$prefix.md5($match_url[0]).".".$match_url[1];
// if (file_exist($imgname)) {
// echo fileGet($imgname);exit;
// }
$img = get_wx_content($match_url[0]);
file_put_contents($imgname, $img);
return $imgname;
}
$article=[
'dd.html'=>'http://www.benshifu.org/txjt/wx/dd.php',
'ft.html'=>'http://www.benshifu.org/txjt/wx/ft.php',
'jj.html'=>'http://www.benshifu.org/txjt/wx/jj.php',
'lhd.html'=>'http://www.benshifu.org/txjt/wx/lhd.php',
'pxd.html'=>'http://www.benshifu.org/txjt/wx/pxd.php',
'wsd.html'=>'http://www.benshifu.org/txjt/wx/wsd.php',
'zsd.html'=>'http://www.benshifu.org/txjt/wx/zsd.php',
'zzdf.html'=>'http://www.benshifu.org/txjt/wx/zzdf.php',
'ag.html'=>'http://www.benshifu.org/txjt/wx/ag.php',
'qsgy.html'=>'http://www.benshifu.org/txjt/wx/qsgy.php',
'wfl.html'=>'http://www.benshifu.org/txjt/wx/wfl.php',
];
foreach ($article as $filename => $url) {
//$url = $article[$x];
echo $filename," : url : ",$url;
$content = get_wx_content($url);
$content = str_replace("data-src", "src", $content);
//208——232 删除25行; 455——463,删除8行
$content = preg_replace_callback('@http://mmbiz.qpic.cn[^\s]+(jpeg|png)@i', 'image_wx_handle', $content,-1);
$content = preg_replace_callback('@https://mmbiz.qpic.cn[^\s]+(jpeg|png)@i', 'image_wx_handle', $content,-1);
file_put_contents($filename, $content);
//echo gzinflate($content);
//echo $content;
echo "\n";
}