Follow Redirect Return Final URL
If you want to scrape a link and follow the redirect(s) to get the final url, you can use the following php function. This will work to get what is behind the redirect of a tracking link, to see where a bit.ly or other shortened url resolves to, etc.
function follow($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FRESH_CONNECT,true); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); curl_setopt($ch, CURLOPT_TIMEOUT, 120); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $result = curl_exec($ch); $header = curl_getinfo($ch); curl_close($ch); return $header['url']; }
About this entry
You’re currently reading “Follow Redirect Return Final URL,” an entry on PHP Functionalism
- Published:
- 11.12.11 / 5pm
- Category:
- Functions, Screen Scraping
- Tags:
No comments
Jump to comment form | comments rss [?] | trackback uri [?]