有时候我们可能会因为某些原因,在不点击百度链接的情况下,获得来源网址真实地址,使用以下方法即可获得。
如上图所示,URL链接为:https://www.baidu.com/link?url=G-zVzAOV_WhO51JGmRbzzoflu9U1WUJOi25-pTad-x1RKXweAHRHIBpHhOtfyH9JeJCn_GZp9jDYblrtxMat3q&wd=&eqid=86f100e5000038e30000000465017667
而实际上,我们想获取的真实URL为:https://www.cdyizhan.com/kaoyan/16929850818064
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); $result = curl_exec($ch); curl_close($ch); //解析数据 $resUrl = ""; if (strpos($result, '<META http-equiv="refresh"') !== false) { preg_match_all( "#content=\"0;URL='(.*)'\">#iUs", $result, $outArr ); if( is_array($outArr[1]) && $outArr[1][0]!="" ){ $resUrl = $outArr[1][0]; } }else{ $listArr = explode("\r\n", $result); foreach ($listArr as $key => $value) { if (strpos($value, 'Location:') !== false) { $resUrl = trim(substr($value, strpos($value,':')+1)); break; } } } echo $resUrl; exit;
标签:baidu,resUrl,ch,URL,outArr,link,result,curl From: https://www.cnblogs.com/vvvkkkooo/p/17700184.html