因为近来豆瓣的开发者平台已经无限期的下线,而且由于很多小程序直接请求豆瓣的接口,导致了豆瓣做了防盗链的,小程序完全不能打开。
但是服务器端用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)
}
})