转载于:https://www.cnblogs.com/xcsn/p/4678304.html
百度输入关键词搜索出现的列表页,点击目标链接,然而跳转的时候却是百度地址,经过百度解析,才真的跳到目标页面。
解决办法:模拟请求,得到真正的地址。
<?php $url = "http://www.baidu.com/link?url=nS2MGJqjJ4zBBpC8yDF8xDh8vibi1lVeE7gGr9UONBu"; $info = parse_url($url); $fp = fsockopen($info['host'], 80,$errno, $errstr, 30); fputs($fp,"GET {$info['path']}?{$info['query']} HTTP/1.1\r\n"); fputs($fp, "Host: {$info['host']}\r\n"); fputs($fp, "Connection: close\r\n\r\n"); $rewrite = ''; while(!feof($fp)) { $line = fgets($fp); if($line != "\r\n" ) { if(strpos($line,'Location:') !== false) { $rewrite = str_replace(array("\r","\n","Location: "),'',$line); } }else { break; } } var_dump($rewrite); //结果显示:string(22) "http://www.google.com/" ?>
将http://www.baidu.com/link?url=nS2MGJqjJ4zBBpC8yDF8xDh8vibi1lVeE7gGr9UONBu解析为http://www.google.com/