Get final redirect with Curl PHP -
i have final redirect url this: https://web.archive.org/web/20070701005218/http://www.maladnews.com/ redirects this: https://web.archive.org/web/20080109064420/http://www.maladnews.com/site%203/malad%20city%20&%20oneida%20county%20news/malad%20city%20&%20oneida%20county%20news.html
i tried answers other stackoverflow answers works other websites not above link.
i've tried extract regular location header:
if(preg_match('#location: (.*)#', $html, $m)) $l = trim($m[1]); and checked javascript way:
preg_match("/window\.location\.replace\('(.*?)'\)/", $html, $m) ? $m[1] : null; please help!
use curl_getinfo() curlinfo_redirect_url or curlinfo_effective_url depending on use case.
curlinfo_redirect_url-curlopt_followlocationoption disabled: redirect url found in last transaction, should requested manually next.curlopt_followlocationoption enabled: empty. redirect url in case available incurlinfo_effective_url
-- http://php.net/manual/en/function.curl-getinfo.php
example:
<?php $url = 'https://google.com/'; $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_followlocation, true); $html = curl_exec($ch); $redirectedurl = curl_getinfo($ch, curlinfo_effective_url); curl_close($ch); echo "original url: " . $url . "\n"; echo "redirected url: " . $redirectedurl . "\n"; when run code, output is:
original url: https://google.com/ redirected url: https://www.google.com/
Comments
Post a Comment