因爲近來豆瓣的開發者平臺已經無限期的下線,而且由於很多小程序直接請求豆瓣的接口,導致了豆瓣做了防盜鏈的,小程序完全不能打開。
但是服務器端用curl發現是可以訪問,那麼我們不如自己做一個反向代理,爲我們自己的小程序提供一箇中轉站。下面不BB了,直接貼代碼
Nginx端:【因爲小程序必須要用https協議,建議小夥伴自己申請一個免費的,華爲雲,阿里雲,騰訊雲都提供】
server {
listen 443;
server_name www.example.com;
ssl on;
index index.html index.htm index.php;
ssl_certificate /aliyun/server/nginx/cert/example.com/www/server.crt;
ssl_certificate_key /aliyun/server/nginx/cert/example.com/www/server.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_store off;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Referer 'no-referrer-when-downgrade';
proxy_set_header User-Agent 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36';
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_pass https://api.douban.com;
}
}
小程序前端的坑:【 'content-type': 'application/json' 已經不支持了,建議換成 'Content-Type': 'json' 不然會報400的錯誤】
wx.request({
url: 'https://www.example.com/v2/movie/top250?start=0&count=2',
data: {},
method: 'get',
header: {
// 'content-type': 'application/json'
'Content-Type': 'json'
},
success: function (res) {
console.log(res.data)
}
})