微信微信官方账号发布的图片已获取,但无法正常显示。提示:此图片来源于微信公众平台,未经允许不得引用。
这是怎么回事?
遇到这个问题是因为微信公众平台对图片采用了防盗链设置。微信对外提供了一个API接口,让我们可以通过授权获取自己微信官方账号中的文章,也可以通过爬虫抓取微信。文章,但微信图片默认不允许对外调用
那么如何解决这个问题呢?
在这里我找到了两个解决方案
第一种
提前在JS中本地加载图片,然后从本地缓存中读取图片
var show img=function(URL){ var frame id=' frame img ' Math . random();window . img=' img id=' img ' src= ' URL '?math . random()' '/script window . onload=function(){ parent . document . getelementbyid( ' ' frame id ' ')。height=document . getelementbyid( ' img ')。高度' px ';} ' '/script ';返回“iframe id=”frame id“src=”http : JavaScript : parent . img;frameBorder='0 '滚动='no '宽度=' 100% '/iframe;}通过各种iframe、表单等跳过防盗链机制。但是这种方法对代码结构有很大的影响,并且与一些移动类库不兼容。
第二种类型
使用背景方法
img class=' form _ logo ' src=' http : { : u(' showMpImg ')}?URL={ $ VO . URL } ' style=' max-width : none ' width=' 160 ' height=' 100 '/display picture public function showing($ URL){ header(' content _ type : image/JPEG ');echo file _ get _ contents($ URL);}第三,用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)applebwebkit/537.36(KHTML,像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);标题(' Content-type : image/jpg ');echo $结果;退出;结果可见一斑,
目前我已经用第二种方法测试过了,代码简单易懂,建议使用。
以上就是本文的全部内容。希望对大家的学习有帮助,支持我们。