微信小程序request获取重定向链接
wx.request({ url: , method:get, header: { content-type: application/x-www-form-urlencoded, } }).onHeadersReceived(res => { console.log(res) if (res.header.Location) { resolve(res.header.Location) } })
通过onHeadersReceived可以获取到请求链接重定向的链接信息,自测只有在安卓手机上有效,在苹果和pc无效。
一般通过后台获取,如php的获取后传回前端
function get_redirect_url($url, $referer="", $timeout = 10) { $redirect_url = false; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_NOBODY, TRUE);//不返回请求体内容 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);//允许请求的链接跳转 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "toolName"=> "B站封面下载", "Accept" => "*/*", "User-Agent" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)", "Connection" => "Keep-Alive")); if ($referer) { curl_setopt($ch, CURLOPT_REFERER, $referer);//设置referer } $content = curl_exec($ch); if(!curl_errno($ch)) { $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);//获取最终请求的url地址 } return $redirect_url; }
上一篇:
uniapp开发微信小程序-2.页面制作