当前位置:首页 > 电脑常识 > 正文

怎么样解决微信文章图片防盗链问题 8090安适门户

11-22 电脑常识

微信对外提供了API接口,让我们可以通过授权的方式获取到自己公众号里面的文章,或者你也可以通过爬虫去抓取微信的文章,但是微信的图片默认是不允许外部调用的
这里我找到了两种方案
第一种
在JS中提前把图片加载到本地,然后从本地缓存中读取图片

var showImg = function (url) { var frameid = 'frameimg' + Math.random(); window.img = '<img src=http://www.8090-sec.com/archives/6175/\'' + url + '?' + Math.random() + 'http://www.8090-sec.com/archives/6175/\' /><script>window.onload = function() { parent.document.getElementById(\'' + frameid + 'http://www.8090-sec.com/archives/6175/\').height = document.getElementById(\'img\').height+\'px\'; }<' + '/script>'; return '<iframe src="javascript:parent.img;" frameBorder="0" scrolling="no"></iframe>'; }

第二种
用PHP模拟浏览器请求

$url = $request->input('url'); $ch = curl_init(); $httpheader = array( 'Host' => 'mmbiz.qpic.cn', 'Connection' => 'keep-alive', 'Pragma' => 'no-cache', 'Cache-Control' => 'no-cache', 'Accept' => 'textml,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8', 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36', 'Accept-Encoding' => 'gzip, deflate, sdch', 'Accept-Language' => 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4' ); $options = array( CURLOPT_HTTPHEADER => $httpheader, CURLOPT_URL => $url, CURLOPT_TIMEOUT => 5, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_RETURNTRANSFER => true ); curl_setopt_array( $ch , $options ); $result = curl_exec( $ch ); curl_close($ch); header('Content-type: image/jpg'); echo $result; exit;

两种方法类似,我目前用的JS的方式,测试过可以用

温馨提示: 本文由杰米博客推荐,转载请保留链接: https://www.jmwww.net/file/pc/13348.html

博客主人杰米WWW
杰米博客,为大家提供seo以及it方面技巧喜欢的朋友收藏哦!
  • 11365文章总数
  • 1378074访问次数
  • 建站天数
  •