《PHP編程:簡單解決微信文章圖片防盜鏈問題》要點:
本文介紹了PHP編程:簡單解決微信文章圖片防盜鏈問題,希望對您有用。如果有疑問,可以聯系我們。
PHP編程微信對外提供了API接口,讓我們可以通過授權的方式獲取到自己公眾號里面的文章,或者你也可以通過爬蟲去抓取微信的文章,但是微信的圖片默認是不允許外部調用的
PHP編程這里我找到了兩種方案
PHP編程第一種
PHP編程在JS中提前把圖片加載到本地,然后從本地緩存中讀取圖片
PHP編程
var showImg = function (url) {
var frameid = 'frameimg' + Math.random();
window.img = '<img id="img" src=\'' + url + '?' + Math.random() + '\' /><script>window.onload = function() { parent.document.getElementById(\'' + frameid + '\').height = document.getElementById(\'img\').height+\'px\'; }<' + '/script>';
return '<iframe id="' + frameid + '" src="parent.img;" frameBorder="0" scrolling="no" width="100%"></iframe>';
}
PHP編程第二種
PHP編程用PHP模擬瀏覽器請求
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;
PHP編程兩種方法類似,我目前用的JS的方式,測試過可以用
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/2289.html